10b57cec5SDimitry Andric /*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\ 20b57cec5SDimitry Andric |* *| 30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| 40b57cec5SDimitry Andric |* Exceptions. *| 50b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information. *| 60b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| 70b57cec5SDimitry Andric |* *| 80b57cec5SDimitry Andric |*===----------------------------------------------------------------------===*| 90b57cec5SDimitry Andric |* *| 100b57cec5SDimitry Andric |* This header provides public interface to an abstract link time optimization*| 110b57cec5SDimitry Andric |* library. LLVM provides an implementation of this interface for use with *| 120b57cec5SDimitry Andric |* llvm bitcode files. *| 130b57cec5SDimitry Andric |* *| 140b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/ 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #ifndef LLVM_C_LTO_H 170b57cec5SDimitry Andric #define LLVM_C_LTO_H 180b57cec5SDimitry Andric 19480093f4SDimitry Andric #include "llvm-c/ExternC.h" 20480093f4SDimitry Andric 210b57cec5SDimitry Andric #ifdef __cplusplus 220b57cec5SDimitry Andric #include <cstddef> 230b57cec5SDimitry Andric #else 240b57cec5SDimitry Andric #include <stddef.h> 250b57cec5SDimitry Andric #endif 260b57cec5SDimitry Andric #include <sys/types.h> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #ifndef __cplusplus 290b57cec5SDimitry Andric #if !defined(_MSC_VER) 300b57cec5SDimitry Andric #include <stdbool.h> 310b57cec5SDimitry Andric typedef bool lto_bool_t; 320b57cec5SDimitry Andric #else 330b57cec5SDimitry Andric /* MSVC in particular does not have anything like _Bool or bool in C, but we can 340b57cec5SDimitry Andric at least make sure the type is the same size. The implementation side will 350b57cec5SDimitry Andric use C++ bool. */ 360b57cec5SDimitry Andric typedef unsigned char lto_bool_t; 370b57cec5SDimitry Andric #endif 380b57cec5SDimitry Andric #else 390b57cec5SDimitry Andric typedef bool lto_bool_t; 400b57cec5SDimitry Andric #endif 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric /** 430b57cec5SDimitry Andric * @defgroup LLVMCLTO LTO 440b57cec5SDimitry Andric * @ingroup LLVMC 450b57cec5SDimitry Andric * 460b57cec5SDimitry Andric * @{ 470b57cec5SDimitry Andric */ 480b57cec5SDimitry Andric 49*349cc55cSDimitry Andric #define LTO_API_VERSION 29 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /** 520b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 530b57cec5SDimitry Andric */ 540b57cec5SDimitry Andric typedef enum { 550b57cec5SDimitry Andric LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */ 560b57cec5SDimitry Andric LTO_SYMBOL_PERMISSIONS_MASK = 0x000000E0, 570b57cec5SDimitry Andric LTO_SYMBOL_PERMISSIONS_CODE = 0x000000A0, 580b57cec5SDimitry Andric LTO_SYMBOL_PERMISSIONS_DATA = 0x000000C0, 590b57cec5SDimitry Andric LTO_SYMBOL_PERMISSIONS_RODATA = 0x00000080, 600b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_MASK = 0x00000700, 610b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_REGULAR = 0x00000100, 620b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_TENTATIVE = 0x00000200, 630b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_WEAK = 0x00000300, 640b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_UNDEFINED = 0x00000400, 650b57cec5SDimitry Andric LTO_SYMBOL_DEFINITION_WEAKUNDEF = 0x00000500, 660b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_MASK = 0x00003800, 670b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_INTERNAL = 0x00000800, 680b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_HIDDEN = 0x00001000, 690b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_PROTECTED = 0x00002000, 700b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_DEFAULT = 0x00001800, 710b57cec5SDimitry Andric LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800, 720b57cec5SDimitry Andric LTO_SYMBOL_COMDAT = 0x00004000, 730b57cec5SDimitry Andric LTO_SYMBOL_ALIAS = 0x00008000 740b57cec5SDimitry Andric } lto_symbol_attributes; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /** 770b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 780b57cec5SDimitry Andric */ 790b57cec5SDimitry Andric typedef enum { 800b57cec5SDimitry Andric LTO_DEBUG_MODEL_NONE = 0, 810b57cec5SDimitry Andric LTO_DEBUG_MODEL_DWARF = 1 820b57cec5SDimitry Andric } lto_debug_model; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /** 850b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 860b57cec5SDimitry Andric */ 870b57cec5SDimitry Andric typedef enum { 880b57cec5SDimitry Andric LTO_CODEGEN_PIC_MODEL_STATIC = 0, 890b57cec5SDimitry Andric LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1, 900b57cec5SDimitry Andric LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2, 910b57cec5SDimitry Andric LTO_CODEGEN_PIC_MODEL_DEFAULT = 3 920b57cec5SDimitry Andric } lto_codegen_model; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric /** opaque reference to a loaded object module */ 950b57cec5SDimitry Andric typedef struct LLVMOpaqueLTOModule *lto_module_t; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric /** opaque reference to a code generator */ 980b57cec5SDimitry Andric typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric /** opaque reference to a thin code generator */ 1010b57cec5SDimitry Andric typedef struct LLVMOpaqueThinLTOCodeGenerator *thinlto_code_gen_t; 1020b57cec5SDimitry Andric 103480093f4SDimitry Andric LLVM_C_EXTERN_C_BEGIN 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric /** 1060b57cec5SDimitry Andric * Returns a printable string. 1070b57cec5SDimitry Andric * 1080b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1090b57cec5SDimitry Andric */ 1100b57cec5SDimitry Andric extern const char* 1110b57cec5SDimitry Andric lto_get_version(void); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric /** 1140b57cec5SDimitry Andric * Returns the last error string or NULL if last operation was successful. 1150b57cec5SDimitry Andric * 1160b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1170b57cec5SDimitry Andric */ 1180b57cec5SDimitry Andric extern const char* 1190b57cec5SDimitry Andric lto_get_error_message(void); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric /** 1220b57cec5SDimitry Andric * Checks if a file is a loadable object file. 1230b57cec5SDimitry Andric * 1240b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1250b57cec5SDimitry Andric */ 1260b57cec5SDimitry Andric extern lto_bool_t 1270b57cec5SDimitry Andric lto_module_is_object_file(const char* path); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric /** 1300b57cec5SDimitry Andric * Checks if a file is a loadable object compiled for requested target. 1310b57cec5SDimitry Andric * 1320b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1330b57cec5SDimitry Andric */ 1340b57cec5SDimitry Andric extern lto_bool_t 1350b57cec5SDimitry Andric lto_module_is_object_file_for_target(const char* path, 1360b57cec5SDimitry Andric const char* target_triple_prefix); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric /** 1390b57cec5SDimitry Andric * Return true if \p Buffer contains a bitcode file with ObjC code (category 1400b57cec5SDimitry Andric * or class) in it. 1410b57cec5SDimitry Andric * 1420b57cec5SDimitry Andric * \since LTO_API_VERSION=20 1430b57cec5SDimitry Andric */ 1440b57cec5SDimitry Andric extern lto_bool_t 1450b57cec5SDimitry Andric lto_module_has_objc_category(const void *mem, size_t length); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric /** 1480b57cec5SDimitry Andric * Checks if a buffer is a loadable object file. 1490b57cec5SDimitry Andric * 1500b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1510b57cec5SDimitry Andric */ 1520b57cec5SDimitry Andric extern lto_bool_t lto_module_is_object_file_in_memory(const void *mem, 1530b57cec5SDimitry Andric size_t length); 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric /** 1560b57cec5SDimitry Andric * Checks if a buffer is a loadable object compiled for requested target. 1570b57cec5SDimitry Andric * 1580b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1590b57cec5SDimitry Andric */ 1600b57cec5SDimitry Andric extern lto_bool_t 1610b57cec5SDimitry Andric lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length, 1620b57cec5SDimitry Andric const char* target_triple_prefix); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric /** 1650b57cec5SDimitry Andric * Loads an object file from disk. 1660b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 1670b57cec5SDimitry Andric * 1680b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1690b57cec5SDimitry Andric */ 1700b57cec5SDimitry Andric extern lto_module_t 1710b57cec5SDimitry Andric lto_module_create(const char* path); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric /** 1740b57cec5SDimitry Andric * Loads an object file from memory. 1750b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 1760b57cec5SDimitry Andric * 1770b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 1780b57cec5SDimitry Andric */ 1790b57cec5SDimitry Andric extern lto_module_t 1800b57cec5SDimitry Andric lto_module_create_from_memory(const void* mem, size_t length); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric /** 1830b57cec5SDimitry Andric * Loads an object file from memory with an extra path argument. 1840b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 1850b57cec5SDimitry Andric * 1860b57cec5SDimitry Andric * \since LTO_API_VERSION=9 1870b57cec5SDimitry Andric */ 1880b57cec5SDimitry Andric extern lto_module_t 1890b57cec5SDimitry Andric lto_module_create_from_memory_with_path(const void* mem, size_t length, 1900b57cec5SDimitry Andric const char *path); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric /** 1930b57cec5SDimitry Andric * Loads an object file in its own context. 1940b57cec5SDimitry Andric * 1950b57cec5SDimitry Andric * Loads an object file in its own LLVMContext. This function call is 1960b57cec5SDimitry Andric * thread-safe. However, modules created this way should not be merged into an 1970b57cec5SDimitry Andric * lto_code_gen_t using \a lto_codegen_add_module(). 1980b57cec5SDimitry Andric * 1990b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 2000b57cec5SDimitry Andric * 2010b57cec5SDimitry Andric * \since LTO_API_VERSION=11 2020b57cec5SDimitry Andric */ 2030b57cec5SDimitry Andric extern lto_module_t 2040b57cec5SDimitry Andric lto_module_create_in_local_context(const void *mem, size_t length, 2050b57cec5SDimitry Andric const char *path); 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric /** 2080b57cec5SDimitry Andric * Loads an object file in the codegen context. 2090b57cec5SDimitry Andric * 2100b57cec5SDimitry Andric * Loads an object file into the same context as \c cg. The module is safe to 2110b57cec5SDimitry Andric * add using \a lto_codegen_add_module(). 2120b57cec5SDimitry Andric * 2130b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 2140b57cec5SDimitry Andric * 2150b57cec5SDimitry Andric * \since LTO_API_VERSION=11 2160b57cec5SDimitry Andric */ 2170b57cec5SDimitry Andric extern lto_module_t 2180b57cec5SDimitry Andric lto_module_create_in_codegen_context(const void *mem, size_t length, 2190b57cec5SDimitry Andric const char *path, lto_code_gen_t cg); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric /** 2220b57cec5SDimitry Andric * Loads an object file from disk. The seek point of fd is not preserved. 2230b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 2240b57cec5SDimitry Andric * 2250b57cec5SDimitry Andric * \since LTO_API_VERSION=5 2260b57cec5SDimitry Andric */ 2270b57cec5SDimitry Andric extern lto_module_t 2280b57cec5SDimitry Andric lto_module_create_from_fd(int fd, const char *path, size_t file_size); 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric /** 2310b57cec5SDimitry Andric * Loads an object file from disk. The seek point of fd is not preserved. 2320b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 2330b57cec5SDimitry Andric * 2340b57cec5SDimitry Andric * \since LTO_API_VERSION=5 2350b57cec5SDimitry Andric */ 2360b57cec5SDimitry Andric extern lto_module_t 2370b57cec5SDimitry Andric lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size, 2380b57cec5SDimitry Andric size_t map_size, off_t offset); 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric /** 2410b57cec5SDimitry Andric * Frees all memory internally allocated by the module. 2420b57cec5SDimitry Andric * Upon return the lto_module_t is no longer valid. 2430b57cec5SDimitry Andric * 2440b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 2450b57cec5SDimitry Andric */ 2460b57cec5SDimitry Andric extern void 2470b57cec5SDimitry Andric lto_module_dispose(lto_module_t mod); 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric /** 2500b57cec5SDimitry Andric * Returns triple string which the object module was compiled under. 2510b57cec5SDimitry Andric * 2520b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 2530b57cec5SDimitry Andric */ 2540b57cec5SDimitry Andric extern const char* 2550b57cec5SDimitry Andric lto_module_get_target_triple(lto_module_t mod); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric /** 2580b57cec5SDimitry Andric * Sets triple string with which the object will be codegened. 2590b57cec5SDimitry Andric * 2600b57cec5SDimitry Andric * \since LTO_API_VERSION=4 2610b57cec5SDimitry Andric */ 2620b57cec5SDimitry Andric extern void 2630b57cec5SDimitry Andric lto_module_set_target_triple(lto_module_t mod, const char *triple); 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric /** 2660b57cec5SDimitry Andric * Returns the number of symbols in the object module. 2670b57cec5SDimitry Andric * 2680b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 2690b57cec5SDimitry Andric */ 2700b57cec5SDimitry Andric extern unsigned int 2710b57cec5SDimitry Andric lto_module_get_num_symbols(lto_module_t mod); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric /** 2740b57cec5SDimitry Andric * Returns the name of the ith symbol in the object module. 2750b57cec5SDimitry Andric * 2760b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 2770b57cec5SDimitry Andric */ 2780b57cec5SDimitry Andric extern const char* 2790b57cec5SDimitry Andric lto_module_get_symbol_name(lto_module_t mod, unsigned int index); 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric /** 2820b57cec5SDimitry Andric * Returns the attributes of the ith symbol in the object module. 2830b57cec5SDimitry Andric * 2840b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 2850b57cec5SDimitry Andric */ 2860b57cec5SDimitry Andric extern lto_symbol_attributes 2870b57cec5SDimitry Andric lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric /** 2900b57cec5SDimitry Andric * Returns the module's linker options. 2910b57cec5SDimitry Andric * 2920b57cec5SDimitry Andric * The linker options may consist of multiple flags. It is the linker's 2930b57cec5SDimitry Andric * responsibility to split the flags using a platform-specific mechanism. 2940b57cec5SDimitry Andric * 2950b57cec5SDimitry Andric * \since LTO_API_VERSION=16 2960b57cec5SDimitry Andric */ 2970b57cec5SDimitry Andric extern const char* 2980b57cec5SDimitry Andric lto_module_get_linkeropts(lto_module_t mod); 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric /** 3015ffd83dbSDimitry Andric * If targeting mach-o on darwin, this function gets the CPU type and subtype 3025ffd83dbSDimitry Andric * that will end up being encoded in the mach-o header. These are the values 3035ffd83dbSDimitry Andric * that can be found in mach/machine.h. 3045ffd83dbSDimitry Andric * 3055ffd83dbSDimitry Andric * \p out_cputype and \p out_cpusubtype must be non-NULL. 3065ffd83dbSDimitry Andric * 3075ffd83dbSDimitry Andric * Returns true on error (check lto_get_error_message() for details). 3085ffd83dbSDimitry Andric * 3095ffd83dbSDimitry Andric * \since LTO_API_VERSION=27 3105ffd83dbSDimitry Andric */ 3115ffd83dbSDimitry Andric extern lto_bool_t lto_module_get_macho_cputype(lto_module_t mod, 3125ffd83dbSDimitry Andric unsigned int *out_cputype, 3135ffd83dbSDimitry Andric unsigned int *out_cpusubtype); 3145ffd83dbSDimitry Andric 3155ffd83dbSDimitry Andric /** 316*349cc55cSDimitry Andric * This function can be used by the linker to check if a given module has 317*349cc55cSDimitry Andric * any constructor or destructor functions. 318*349cc55cSDimitry Andric * 319*349cc55cSDimitry Andric * Returns true if the module has either the @llvm.global_ctors or the 320*349cc55cSDimitry Andric * @llvm.global_dtors symbol. Otherwise returns false. 321*349cc55cSDimitry Andric * 322*349cc55cSDimitry Andric * \since LTO_API_VERSION=29 323*349cc55cSDimitry Andric */ 324*349cc55cSDimitry Andric extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod); 325*349cc55cSDimitry Andric /** 3260b57cec5SDimitry Andric * Diagnostic severity. 3270b57cec5SDimitry Andric * 3280b57cec5SDimitry Andric * \since LTO_API_VERSION=7 3290b57cec5SDimitry Andric */ 3300b57cec5SDimitry Andric typedef enum { 3310b57cec5SDimitry Andric LTO_DS_ERROR = 0, 3320b57cec5SDimitry Andric LTO_DS_WARNING = 1, 3330b57cec5SDimitry Andric LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10. 3340b57cec5SDimitry Andric LTO_DS_NOTE = 2 3350b57cec5SDimitry Andric } lto_codegen_diagnostic_severity_t; 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric /** 3380b57cec5SDimitry Andric * Diagnostic handler type. 3390b57cec5SDimitry Andric * \p severity defines the severity. 3400b57cec5SDimitry Andric * \p diag is the actual diagnostic. 3410b57cec5SDimitry Andric * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '. 3420b57cec5SDimitry Andric * \p ctxt is used to pass the context set with the diagnostic handler. 3430b57cec5SDimitry Andric * 3440b57cec5SDimitry Andric * \since LTO_API_VERSION=7 3450b57cec5SDimitry Andric */ 3460b57cec5SDimitry Andric typedef void (*lto_diagnostic_handler_t)( 3470b57cec5SDimitry Andric lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt); 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric /** 3500b57cec5SDimitry Andric * Set a diagnostic handler and the related context (void *). 3510b57cec5SDimitry Andric * This is more general than lto_get_error_message, as the diagnostic handler 3520b57cec5SDimitry Andric * can be called at anytime within lto. 3530b57cec5SDimitry Andric * 3540b57cec5SDimitry Andric * \since LTO_API_VERSION=7 3550b57cec5SDimitry Andric */ 3560b57cec5SDimitry Andric extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t, 3570b57cec5SDimitry Andric lto_diagnostic_handler_t, 3580b57cec5SDimitry Andric void *); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric /** 3610b57cec5SDimitry Andric * Instantiates a code generator. 3620b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 3630b57cec5SDimitry Andric * 3640b57cec5SDimitry Andric * All modules added using \a lto_codegen_add_module() must have been created 3650b57cec5SDimitry Andric * in the same context as the codegen. 3660b57cec5SDimitry Andric * 3670b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 3680b57cec5SDimitry Andric */ 3690b57cec5SDimitry Andric extern lto_code_gen_t 3700b57cec5SDimitry Andric lto_codegen_create(void); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric /** 3730b57cec5SDimitry Andric * Instantiate a code generator in its own context. 3740b57cec5SDimitry Andric * 3750b57cec5SDimitry Andric * Instantiates a code generator in its own context. Modules added via \a 3760b57cec5SDimitry Andric * lto_codegen_add_module() must have all been created in the same context, 3770b57cec5SDimitry Andric * using \a lto_module_create_in_codegen_context(). 3780b57cec5SDimitry Andric * 3790b57cec5SDimitry Andric * \since LTO_API_VERSION=11 3800b57cec5SDimitry Andric */ 3810b57cec5SDimitry Andric extern lto_code_gen_t 3820b57cec5SDimitry Andric lto_codegen_create_in_local_context(void); 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric /** 3850b57cec5SDimitry Andric * Frees all code generator and all memory it internally allocated. 3860b57cec5SDimitry Andric * Upon return the lto_code_gen_t is no longer valid. 3870b57cec5SDimitry Andric * 3880b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 3890b57cec5SDimitry Andric */ 3900b57cec5SDimitry Andric extern void 3910b57cec5SDimitry Andric lto_codegen_dispose(lto_code_gen_t); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric /** 3940b57cec5SDimitry Andric * Add an object module to the set of modules for which code will be generated. 3950b57cec5SDimitry Andric * Returns true on error (check lto_get_error_message() for details). 3960b57cec5SDimitry Andric * 3970b57cec5SDimitry Andric * \c cg and \c mod must both be in the same context. See \a 3980b57cec5SDimitry Andric * lto_codegen_create_in_local_context() and \a 3990b57cec5SDimitry Andric * lto_module_create_in_codegen_context(). 4000b57cec5SDimitry Andric * 4010b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 4020b57cec5SDimitry Andric */ 4030b57cec5SDimitry Andric extern lto_bool_t 4040b57cec5SDimitry Andric lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod); 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric /** 4070b57cec5SDimitry Andric * Sets the object module for code generation. This will transfer the ownership 4080b57cec5SDimitry Andric * of the module to the code generator. 4090b57cec5SDimitry Andric * 4100b57cec5SDimitry Andric * \c cg and \c mod must both be in the same context. 4110b57cec5SDimitry Andric * 4120b57cec5SDimitry Andric * \since LTO_API_VERSION=13 4130b57cec5SDimitry Andric */ 4140b57cec5SDimitry Andric extern void 4150b57cec5SDimitry Andric lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric /** 4180b57cec5SDimitry Andric * Sets if debug info should be generated. 4190b57cec5SDimitry Andric * Returns true on error (check lto_get_error_message() for details). 4200b57cec5SDimitry Andric * 4210b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 4220b57cec5SDimitry Andric */ 4230b57cec5SDimitry Andric extern lto_bool_t 4240b57cec5SDimitry Andric lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric /** 4270b57cec5SDimitry Andric * Sets which PIC code model to generated. 4280b57cec5SDimitry Andric * Returns true on error (check lto_get_error_message() for details). 4290b57cec5SDimitry Andric * 4300b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 4310b57cec5SDimitry Andric */ 4320b57cec5SDimitry Andric extern lto_bool_t 4330b57cec5SDimitry Andric lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model); 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric /** 4360b57cec5SDimitry Andric * Sets the cpu to generate code for. 4370b57cec5SDimitry Andric * 4380b57cec5SDimitry Andric * \since LTO_API_VERSION=4 4390b57cec5SDimitry Andric */ 4400b57cec5SDimitry Andric extern void 4410b57cec5SDimitry Andric lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu); 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric /** 4440b57cec5SDimitry Andric * Sets the location of the assembler tool to run. If not set, libLTO 4450b57cec5SDimitry Andric * will use gcc to invoke the assembler. 4460b57cec5SDimitry Andric * 4470b57cec5SDimitry Andric * \since LTO_API_VERSION=3 4480b57cec5SDimitry Andric */ 4490b57cec5SDimitry Andric extern void 4500b57cec5SDimitry Andric lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric /** 4530b57cec5SDimitry Andric * Sets extra arguments that libLTO should pass to the assembler. 4540b57cec5SDimitry Andric * 4550b57cec5SDimitry Andric * \since LTO_API_VERSION=4 4560b57cec5SDimitry Andric */ 4570b57cec5SDimitry Andric extern void 4580b57cec5SDimitry Andric lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, 4590b57cec5SDimitry Andric int nargs); 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric /** 4620b57cec5SDimitry Andric * Adds to a list of all global symbols that must exist in the final generated 4630b57cec5SDimitry Andric * code. If a function is not listed there, it might be inlined into every usage 4640b57cec5SDimitry Andric * and optimized away. 4650b57cec5SDimitry Andric * 4660b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 4670b57cec5SDimitry Andric */ 4680b57cec5SDimitry Andric extern void 4690b57cec5SDimitry Andric lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric /** 4720b57cec5SDimitry Andric * Writes a new object file at the specified path that contains the 4730b57cec5SDimitry Andric * merged contents of all modules added so far. 4740b57cec5SDimitry Andric * Returns true on error (check lto_get_error_message() for details). 4750b57cec5SDimitry Andric * 4760b57cec5SDimitry Andric * \since LTO_API_VERSION=5 4770b57cec5SDimitry Andric */ 4780b57cec5SDimitry Andric extern lto_bool_t 4790b57cec5SDimitry Andric lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric /** 4820b57cec5SDimitry Andric * Generates code for all added modules into one native object file. 4830b57cec5SDimitry Andric * This calls lto_codegen_optimize then lto_codegen_compile_optimized. 4840b57cec5SDimitry Andric * 4850b57cec5SDimitry Andric * On success returns a pointer to a generated mach-o/ELF buffer and 4860b57cec5SDimitry Andric * length set to the buffer size. The buffer is owned by the 4870b57cec5SDimitry Andric * lto_code_gen_t and will be freed when lto_codegen_dispose() 4880b57cec5SDimitry Andric * is called, or lto_codegen_compile() is called again. 4890b57cec5SDimitry Andric * On failure, returns NULL (check lto_get_error_message() for details). 4900b57cec5SDimitry Andric * 4910b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 4920b57cec5SDimitry Andric */ 4930b57cec5SDimitry Andric extern const void* 4940b57cec5SDimitry Andric lto_codegen_compile(lto_code_gen_t cg, size_t* length); 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric /** 4970b57cec5SDimitry Andric * Generates code for all added modules into one native object file. 4980b57cec5SDimitry Andric * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead 4990b57cec5SDimitry Andric * of returning a generated mach-o/ELF buffer, it writes to a file). 5000b57cec5SDimitry Andric * 5010b57cec5SDimitry Andric * The name of the file is written to name. Returns true on error. 5020b57cec5SDimitry Andric * 5030b57cec5SDimitry Andric * \since LTO_API_VERSION=5 5040b57cec5SDimitry Andric */ 5050b57cec5SDimitry Andric extern lto_bool_t 5060b57cec5SDimitry Andric lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric /** 5090b57cec5SDimitry Andric * Runs optimization for the merged module. Returns true on error. 5100b57cec5SDimitry Andric * 5110b57cec5SDimitry Andric * \since LTO_API_VERSION=12 5120b57cec5SDimitry Andric */ 5130b57cec5SDimitry Andric extern lto_bool_t 5140b57cec5SDimitry Andric lto_codegen_optimize(lto_code_gen_t cg); 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric /** 5170b57cec5SDimitry Andric * Generates code for the optimized merged module into one native object file. 5180b57cec5SDimitry Andric * It will not run any IR optimizations on the merged module. 5190b57cec5SDimitry Andric * 5200b57cec5SDimitry Andric * On success returns a pointer to a generated mach-o/ELF buffer and length set 5210b57cec5SDimitry Andric * to the buffer size. The buffer is owned by the lto_code_gen_t and will be 5220b57cec5SDimitry Andric * freed when lto_codegen_dispose() is called, or 5230b57cec5SDimitry Andric * lto_codegen_compile_optimized() is called again. On failure, returns NULL 5240b57cec5SDimitry Andric * (check lto_get_error_message() for details). 5250b57cec5SDimitry Andric * 5260b57cec5SDimitry Andric * \since LTO_API_VERSION=12 5270b57cec5SDimitry Andric */ 5280b57cec5SDimitry Andric extern const void* 5290b57cec5SDimitry Andric lto_codegen_compile_optimized(lto_code_gen_t cg, size_t* length); 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric /** 5320b57cec5SDimitry Andric * Returns the runtime API version. 5330b57cec5SDimitry Andric * 5340b57cec5SDimitry Andric * \since LTO_API_VERSION=12 5350b57cec5SDimitry Andric */ 5360b57cec5SDimitry Andric extern unsigned int 5370b57cec5SDimitry Andric lto_api_version(void); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric /** 540fe6060f1SDimitry Andric * Parses options immediately, making them available as early as possible. For 541fe6060f1SDimitry Andric * example during executing codegen::InitTargetOptionsFromCodeGenFlags. Since 542fe6060f1SDimitry Andric * parsing shud only happen once, only one of lto_codegen_debug_options or 543fe6060f1SDimitry Andric * lto_set_debug_options should be called. 544fe6060f1SDimitry Andric * 545fe6060f1SDimitry Andric * This function takes one or more options separated by spaces. 546fe6060f1SDimitry Andric * Warning: passing file paths through this function may confuse the argument 547fe6060f1SDimitry Andric * parser if the paths contain spaces. 548fe6060f1SDimitry Andric * 549fe6060f1SDimitry Andric * \since LTO_API_VERSION=28 550fe6060f1SDimitry Andric */ 551fe6060f1SDimitry Andric extern void lto_set_debug_options(const char *const *options, int number); 552fe6060f1SDimitry Andric 553fe6060f1SDimitry Andric /** 554fe6060f1SDimitry Andric * Sets options to help debug codegen bugs. Since parsing shud only happen once, 555fe6060f1SDimitry Andric * only one of lto_codegen_debug_options or lto_set_debug_options 556fe6060f1SDimitry Andric * should be called. 5570b57cec5SDimitry Andric * 558480093f4SDimitry Andric * This function takes one or more options separated by spaces. 559480093f4SDimitry Andric * Warning: passing file paths through this function may confuse the argument 560480093f4SDimitry Andric * parser if the paths contain spaces. 561480093f4SDimitry Andric * 5620b57cec5SDimitry Andric * \since prior to LTO_API_VERSION=3 5630b57cec5SDimitry Andric */ 5640b57cec5SDimitry Andric extern void 5650b57cec5SDimitry Andric lto_codegen_debug_options(lto_code_gen_t cg, const char *); 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric /** 568480093f4SDimitry Andric * Same as the previous function, but takes every option separately through an 569480093f4SDimitry Andric * array. 570480093f4SDimitry Andric * 571480093f4SDimitry Andric * \since prior to LTO_API_VERSION=26 572480093f4SDimitry Andric */ 573480093f4SDimitry Andric extern void lto_codegen_debug_options_array(lto_code_gen_t cg, 574480093f4SDimitry Andric const char *const *, int number); 575480093f4SDimitry Andric 576480093f4SDimitry Andric /** 5770b57cec5SDimitry Andric * Initializes LLVM disassemblers. 5780b57cec5SDimitry Andric * FIXME: This doesn't really belong here. 5790b57cec5SDimitry Andric * 5800b57cec5SDimitry Andric * \since LTO_API_VERSION=5 5810b57cec5SDimitry Andric */ 5820b57cec5SDimitry Andric extern void 5830b57cec5SDimitry Andric lto_initialize_disassembler(void); 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric /** 5860b57cec5SDimitry Andric * Sets if we should run internalize pass during optimization and code 5870b57cec5SDimitry Andric * generation. 5880b57cec5SDimitry Andric * 5890b57cec5SDimitry Andric * \since LTO_API_VERSION=14 5900b57cec5SDimitry Andric */ 5910b57cec5SDimitry Andric extern void 5920b57cec5SDimitry Andric lto_codegen_set_should_internalize(lto_code_gen_t cg, 5930b57cec5SDimitry Andric lto_bool_t ShouldInternalize); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric /** 5960b57cec5SDimitry Andric * Set whether to embed uselists in bitcode. 5970b57cec5SDimitry Andric * 5980b57cec5SDimitry Andric * Sets whether \a lto_codegen_write_merged_modules() should embed uselists in 5990b57cec5SDimitry Andric * output bitcode. This should be turned on for all -save-temps output. 6000b57cec5SDimitry Andric * 6010b57cec5SDimitry Andric * \since LTO_API_VERSION=15 6020b57cec5SDimitry Andric */ 6030b57cec5SDimitry Andric extern void 6040b57cec5SDimitry Andric lto_codegen_set_should_embed_uselists(lto_code_gen_t cg, 6050b57cec5SDimitry Andric lto_bool_t ShouldEmbedUselists); 6060b57cec5SDimitry Andric 6078bcb0991SDimitry Andric /** Opaque reference to an LTO input file */ 6088bcb0991SDimitry Andric typedef struct LLVMOpaqueLTOInput *lto_input_t; 6098bcb0991SDimitry Andric 6108bcb0991SDimitry Andric /** 6118bcb0991SDimitry Andric * Creates an LTO input file from a buffer. The path 6128bcb0991SDimitry Andric * argument is used for diagnotics as this function 6138bcb0991SDimitry Andric * otherwise does not know which file the given buffer 6148bcb0991SDimitry Andric * is associated with. 6158bcb0991SDimitry Andric * 6168bcb0991SDimitry Andric * \since LTO_API_VERSION=24 6178bcb0991SDimitry Andric */ 6188bcb0991SDimitry Andric extern lto_input_t lto_input_create(const void *buffer, 6198bcb0991SDimitry Andric size_t buffer_size, 6208bcb0991SDimitry Andric const char *path); 6218bcb0991SDimitry Andric 6228bcb0991SDimitry Andric /** 6238bcb0991SDimitry Andric * Frees all memory internally allocated by the LTO input file. 6248bcb0991SDimitry Andric * Upon return the lto_module_t is no longer valid. 6258bcb0991SDimitry Andric * 6268bcb0991SDimitry Andric * \since LTO_API_VERSION=24 6278bcb0991SDimitry Andric */ 6288bcb0991SDimitry Andric extern void lto_input_dispose(lto_input_t input); 6298bcb0991SDimitry Andric 6308bcb0991SDimitry Andric /** 6318bcb0991SDimitry Andric * Returns the number of dependent library specifiers 6328bcb0991SDimitry Andric * for the given LTO input file. 6338bcb0991SDimitry Andric * 6348bcb0991SDimitry Andric * \since LTO_API_VERSION=24 6358bcb0991SDimitry Andric */ 6368bcb0991SDimitry Andric extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input); 6378bcb0991SDimitry Andric 6388bcb0991SDimitry Andric /** 6398bcb0991SDimitry Andric * Returns the ith dependent library specifier 6408bcb0991SDimitry Andric * for the given LTO input file. The returned 6418bcb0991SDimitry Andric * string is not null-terminated. 6428bcb0991SDimitry Andric * 6438bcb0991SDimitry Andric * \since LTO_API_VERSION=24 6448bcb0991SDimitry Andric */ 6458bcb0991SDimitry Andric extern const char * lto_input_get_dependent_library(lto_input_t input, 6468bcb0991SDimitry Andric size_t index, 6478bcb0991SDimitry Andric size_t *size); 6488bcb0991SDimitry Andric 6498bcb0991SDimitry Andric /** 6508bcb0991SDimitry Andric * Returns the list of libcall symbols that can be generated by LTO 6518bcb0991SDimitry Andric * that might not be visible from the symbol table of bitcode files. 6528bcb0991SDimitry Andric * 6538bcb0991SDimitry Andric * \since prior to LTO_API_VERSION=25 6548bcb0991SDimitry Andric */ 6558bcb0991SDimitry Andric extern const char *const *lto_runtime_lib_symbols_list(size_t *size); 6568bcb0991SDimitry Andric 6570b57cec5SDimitry Andric /** 6580b57cec5SDimitry Andric * @} // endgoup LLVMCLTO 6590b57cec5SDimitry Andric * @defgroup LLVMCTLTO ThinLTO 6600b57cec5SDimitry Andric * @ingroup LLVMC 6610b57cec5SDimitry Andric * 6620b57cec5SDimitry Andric * @{ 6630b57cec5SDimitry Andric */ 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric /** 6660b57cec5SDimitry Andric * Type to wrap a single object returned by ThinLTO. 6670b57cec5SDimitry Andric * 6680b57cec5SDimitry Andric * \since LTO_API_VERSION=18 6690b57cec5SDimitry Andric */ 6700b57cec5SDimitry Andric typedef struct { 6710b57cec5SDimitry Andric const char *Buffer; 6720b57cec5SDimitry Andric size_t Size; 6730b57cec5SDimitry Andric } LTOObjectBuffer; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric /** 6760b57cec5SDimitry Andric * Instantiates a ThinLTO code generator. 6770b57cec5SDimitry Andric * Returns NULL on error (check lto_get_error_message() for details). 6780b57cec5SDimitry Andric * 6790b57cec5SDimitry Andric * 6800b57cec5SDimitry Andric * The ThinLTOCodeGenerator is not intended to be reuse for multiple 6810b57cec5SDimitry Andric * compilation: the model is that the client adds modules to the generator and 6820b57cec5SDimitry Andric * ask to perform the ThinLTO optimizations / codegen, and finally destroys the 6830b57cec5SDimitry Andric * codegenerator. 6840b57cec5SDimitry Andric * 6850b57cec5SDimitry Andric * \since LTO_API_VERSION=18 6860b57cec5SDimitry Andric */ 6870b57cec5SDimitry Andric extern thinlto_code_gen_t thinlto_create_codegen(void); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric /** 6900b57cec5SDimitry Andric * Frees the generator and all memory it internally allocated. 6910b57cec5SDimitry Andric * Upon return the thinlto_code_gen_t is no longer valid. 6920b57cec5SDimitry Andric * 6930b57cec5SDimitry Andric * \since LTO_API_VERSION=18 6940b57cec5SDimitry Andric */ 6950b57cec5SDimitry Andric extern void thinlto_codegen_dispose(thinlto_code_gen_t cg); 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric /** 6980b57cec5SDimitry Andric * Add a module to a ThinLTO code generator. Identifier has to be unique among 6990b57cec5SDimitry Andric * all the modules in a code generator. The data buffer stays owned by the 7000b57cec5SDimitry Andric * client, and is expected to be available for the entire lifetime of the 7010b57cec5SDimitry Andric * thinlto_code_gen_t it is added to. 7020b57cec5SDimitry Andric * 7030b57cec5SDimitry Andric * On failure, returns NULL (check lto_get_error_message() for details). 7040b57cec5SDimitry Andric * 7050b57cec5SDimitry Andric * 7060b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7070b57cec5SDimitry Andric */ 7080b57cec5SDimitry Andric extern void thinlto_codegen_add_module(thinlto_code_gen_t cg, 7090b57cec5SDimitry Andric const char *identifier, const char *data, 7100b57cec5SDimitry Andric int length); 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric /** 7130b57cec5SDimitry Andric * Optimize and codegen all the modules added to the codegenerator using 7140b57cec5SDimitry Andric * ThinLTO. Resulting objects are accessible using thinlto_module_get_object(). 7150b57cec5SDimitry Andric * 7160b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7170b57cec5SDimitry Andric */ 7180b57cec5SDimitry Andric extern void thinlto_codegen_process(thinlto_code_gen_t cg); 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric /** 7210b57cec5SDimitry Andric * Returns the number of object files produced by the ThinLTO CodeGenerator. 7220b57cec5SDimitry Andric * 7230b57cec5SDimitry Andric * It usually matches the number of input files, but this is not a guarantee of 7240b57cec5SDimitry Andric * the API and may change in future implementation, so the client should not 7250b57cec5SDimitry Andric * assume it. 7260b57cec5SDimitry Andric * 7270b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7280b57cec5SDimitry Andric */ 7290b57cec5SDimitry Andric extern unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg); 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric /** 7320b57cec5SDimitry Andric * Returns a reference to the ith object file produced by the ThinLTO 7330b57cec5SDimitry Andric * CodeGenerator. 7340b57cec5SDimitry Andric * 7350b57cec5SDimitry Andric * Client should use \p thinlto_module_get_num_objects() to get the number of 7360b57cec5SDimitry Andric * available objects. 7370b57cec5SDimitry Andric * 7380b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7390b57cec5SDimitry Andric */ 7400b57cec5SDimitry Andric extern LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg, 7410b57cec5SDimitry Andric unsigned int index); 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric /** 7440b57cec5SDimitry Andric * Returns the number of object files produced by the ThinLTO CodeGenerator. 7450b57cec5SDimitry Andric * 7460b57cec5SDimitry Andric * It usually matches the number of input files, but this is not a guarantee of 7470b57cec5SDimitry Andric * the API and may change in future implementation, so the client should not 7480b57cec5SDimitry Andric * assume it. 7490b57cec5SDimitry Andric * 7500b57cec5SDimitry Andric * \since LTO_API_VERSION=21 7510b57cec5SDimitry Andric */ 7520b57cec5SDimitry Andric unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg); 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric /** 7550b57cec5SDimitry Andric * Returns the path to the ith object file produced by the ThinLTO 7560b57cec5SDimitry Andric * CodeGenerator. 7570b57cec5SDimitry Andric * 7580b57cec5SDimitry Andric * Client should use \p thinlto_module_get_num_object_files() to get the number 7590b57cec5SDimitry Andric * of available objects. 7600b57cec5SDimitry Andric * 7610b57cec5SDimitry Andric * \since LTO_API_VERSION=21 7620b57cec5SDimitry Andric */ 7630b57cec5SDimitry Andric const char *thinlto_module_get_object_file(thinlto_code_gen_t cg, 7640b57cec5SDimitry Andric unsigned int index); 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric /** 7670b57cec5SDimitry Andric * Sets which PIC code model to generate. 7680b57cec5SDimitry Andric * Returns true on error (check lto_get_error_message() for details). 7690b57cec5SDimitry Andric * 7700b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7710b57cec5SDimitry Andric */ 7720b57cec5SDimitry Andric extern lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg, 7730b57cec5SDimitry Andric lto_codegen_model); 7740b57cec5SDimitry Andric 7750b57cec5SDimitry Andric /** 7760b57cec5SDimitry Andric * Sets the path to a directory to use as a storage for temporary bitcode files. 7770b57cec5SDimitry Andric * The intention is to make the bitcode files available for debugging at various 7780b57cec5SDimitry Andric * stage of the pipeline. 7790b57cec5SDimitry Andric * 7800b57cec5SDimitry Andric * \since LTO_API_VERSION=18 7810b57cec5SDimitry Andric */ 7820b57cec5SDimitry Andric extern void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg, 7830b57cec5SDimitry Andric const char *save_temps_dir); 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric /** 7860b57cec5SDimitry Andric * Set the path to a directory where to save generated object files. This 7870b57cec5SDimitry Andric * path can be used by a linker to request on-disk files instead of in-memory 7880b57cec5SDimitry Andric * buffers. When set, results are available through 7890b57cec5SDimitry Andric * thinlto_module_get_object_file() instead of thinlto_module_get_object(). 7900b57cec5SDimitry Andric * 7910b57cec5SDimitry Andric * \since LTO_API_VERSION=21 7920b57cec5SDimitry Andric */ 7930b57cec5SDimitry Andric void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg, 7940b57cec5SDimitry Andric const char *save_temps_dir); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric /** 7970b57cec5SDimitry Andric * Sets the cpu to generate code for. 7980b57cec5SDimitry Andric * 7990b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8000b57cec5SDimitry Andric */ 8010b57cec5SDimitry Andric extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu); 8020b57cec5SDimitry Andric 8030b57cec5SDimitry Andric /** 8040b57cec5SDimitry Andric * Disable CodeGen, only run the stages till codegen and stop. The output will 8050b57cec5SDimitry Andric * be bitcode. 8060b57cec5SDimitry Andric * 8070b57cec5SDimitry Andric * \since LTO_API_VERSION=19 8080b57cec5SDimitry Andric */ 8090b57cec5SDimitry Andric extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg, 8100b57cec5SDimitry Andric lto_bool_t disable); 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric /** 8130b57cec5SDimitry Andric * Perform CodeGen only: disable all other stages. 8140b57cec5SDimitry Andric * 8150b57cec5SDimitry Andric * \since LTO_API_VERSION=19 8160b57cec5SDimitry Andric */ 8170b57cec5SDimitry Andric extern void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg, 8180b57cec5SDimitry Andric lto_bool_t codegen_only); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric /** 8210b57cec5SDimitry Andric * Parse -mllvm style debug options. 8220b57cec5SDimitry Andric * 8230b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8240b57cec5SDimitry Andric */ 8250b57cec5SDimitry Andric extern void thinlto_debug_options(const char *const *options, int number); 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric /** 8280b57cec5SDimitry Andric * Test if a module has support for ThinLTO linking. 8290b57cec5SDimitry Andric * 8300b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8310b57cec5SDimitry Andric */ 8320b57cec5SDimitry Andric extern lto_bool_t lto_module_is_thinlto(lto_module_t mod); 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric /** 8350b57cec5SDimitry Andric * Adds a symbol to the list of global symbols that must exist in the final 8360b57cec5SDimitry Andric * generated code. If a function is not listed there, it might be inlined into 8370b57cec5SDimitry Andric * every usage and optimized away. For every single module, the functions 8380b57cec5SDimitry Andric * referenced from code outside of the ThinLTO modules need to be added here. 8390b57cec5SDimitry Andric * 8400b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8410b57cec5SDimitry Andric */ 8420b57cec5SDimitry Andric extern void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg, 8430b57cec5SDimitry Andric const char *name, 8440b57cec5SDimitry Andric int length); 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric /** 8470b57cec5SDimitry Andric * Adds a symbol to the list of global symbols that are cross-referenced between 8480b57cec5SDimitry Andric * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every 8490b57cec5SDimitry Andric * references from a ThinLTO module to this symbol is optimized away, then 8500b57cec5SDimitry Andric * the symbol can be discarded. 8510b57cec5SDimitry Andric * 8520b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8530b57cec5SDimitry Andric */ 8540b57cec5SDimitry Andric extern void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg, 8550b57cec5SDimitry Andric const char *name, 8560b57cec5SDimitry Andric int length); 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric /** 8590b57cec5SDimitry Andric * @} // endgoup LLVMCTLTO 8600b57cec5SDimitry Andric * @defgroup LLVMCTLTO_CACHING ThinLTO Cache Control 8610b57cec5SDimitry Andric * @ingroup LLVMCTLTO 8620b57cec5SDimitry Andric * 8630b57cec5SDimitry Andric * These entry points control the ThinLTO cache. The cache is intended to 8640b57cec5SDimitry Andric * support incremental builds, and thus needs to be persistent across builds. 8650b57cec5SDimitry Andric * The client enables the cache by supplying a path to an existing directory. 8660b57cec5SDimitry Andric * The code generator will use this to store objects files that may be reused 8670b57cec5SDimitry Andric * during a subsequent build. 8680b57cec5SDimitry Andric * To avoid filling the disk space, a few knobs are provided: 8690b57cec5SDimitry Andric * - The pruning interval limits the frequency at which the garbage collector 8700b57cec5SDimitry Andric * will try to scan the cache directory to prune expired entries. 8710b57cec5SDimitry Andric * Setting to a negative number disables the pruning. 8720b57cec5SDimitry Andric * - The pruning expiration time indicates to the garbage collector how old an 8730b57cec5SDimitry Andric * entry needs to be to be removed. 8740b57cec5SDimitry Andric * - Finally, the garbage collector can be instructed to prune the cache until 8750b57cec5SDimitry Andric * the occupied space goes below a threshold. 8760b57cec5SDimitry Andric * @{ 8770b57cec5SDimitry Andric */ 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric /** 8800b57cec5SDimitry Andric * Sets the path to a directory to use as a cache storage for incremental build. 8810b57cec5SDimitry Andric * Setting this activates caching. 8820b57cec5SDimitry Andric * 8830b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8840b57cec5SDimitry Andric */ 8850b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg, 8860b57cec5SDimitry Andric const char *cache_dir); 8870b57cec5SDimitry Andric 8880b57cec5SDimitry Andric /** 8890b57cec5SDimitry Andric * Sets the cache pruning interval (in seconds). A negative value disables the 8900b57cec5SDimitry Andric * pruning. An unspecified default value will be applied, and a value of 0 will 8910b57cec5SDimitry Andric * force prunning to occur. 8920b57cec5SDimitry Andric * 8930b57cec5SDimitry Andric * \since LTO_API_VERSION=18 8940b57cec5SDimitry Andric */ 8950b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg, 8960b57cec5SDimitry Andric int interval); 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric /** 8990b57cec5SDimitry Andric * Sets the maximum cache size that can be persistent across build, in terms of 9000b57cec5SDimitry Andric * percentage of the available space on the disk. Set to 100 to indicate 9010b57cec5SDimitry Andric * no limit, 50 to indicate that the cache size will not be left over half the 9020b57cec5SDimitry Andric * available space. A value over 100 will be reduced to 100, a value of 0 will 9030b57cec5SDimitry Andric * be ignored. An unspecified default value will be applied. 9040b57cec5SDimitry Andric * 9050b57cec5SDimitry Andric * The formula looks like: 9060b57cec5SDimitry Andric * AvailableSpace = FreeSpace + ExistingCacheSize 9070b57cec5SDimitry Andric * NewCacheSize = AvailableSpace * P/100 9080b57cec5SDimitry Andric * 9090b57cec5SDimitry Andric * \since LTO_API_VERSION=18 9100b57cec5SDimitry Andric */ 9110b57cec5SDimitry Andric extern void thinlto_codegen_set_final_cache_size_relative_to_available_space( 9120b57cec5SDimitry Andric thinlto_code_gen_t cg, unsigned percentage); 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric /** 9150b57cec5SDimitry Andric * Sets the expiration (in seconds) for an entry in the cache. An unspecified 9160b57cec5SDimitry Andric * default value will be applied. A value of 0 will be ignored. 9170b57cec5SDimitry Andric * 9180b57cec5SDimitry Andric * \since LTO_API_VERSION=18 9190b57cec5SDimitry Andric */ 9200b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg, 9210b57cec5SDimitry Andric unsigned expiration); 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric /** 9240b57cec5SDimitry Andric * Sets the maximum size of the cache directory (in bytes). A value over the 9250b57cec5SDimitry Andric * amount of available space on the disk will be reduced to the amount of 9260b57cec5SDimitry Andric * available space. An unspecified default value will be applied. A value of 0 9270b57cec5SDimitry Andric * will be ignored. 9280b57cec5SDimitry Andric * 9290b57cec5SDimitry Andric * \since LTO_API_VERSION=22 9300b57cec5SDimitry Andric */ 9310b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg, 9320b57cec5SDimitry Andric unsigned max_size_bytes); 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric /** 9350b57cec5SDimitry Andric * Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in 9360b57cec5SDimitry Andric * megabytes (2^20 bytes). 9370b57cec5SDimitry Andric * 9380b57cec5SDimitry Andric * \since LTO_API_VERSION=23 9390b57cec5SDimitry Andric */ 9400b57cec5SDimitry Andric extern void 9410b57cec5SDimitry Andric thinlto_codegen_set_cache_size_megabytes(thinlto_code_gen_t cg, 9420b57cec5SDimitry Andric unsigned max_size_megabytes); 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric /** 9450b57cec5SDimitry Andric * Sets the maximum number of files in the cache directory. An unspecified 9460b57cec5SDimitry Andric * default value will be applied. A value of 0 will be ignored. 9470b57cec5SDimitry Andric * 9480b57cec5SDimitry Andric * \since LTO_API_VERSION=22 9490b57cec5SDimitry Andric */ 9500b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg, 9510b57cec5SDimitry Andric unsigned max_size_files); 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric /** 9540b57cec5SDimitry Andric * @} // endgroup LLVMCTLTO_CACHING 9550b57cec5SDimitry Andric */ 9560b57cec5SDimitry Andric 957480093f4SDimitry Andric LLVM_C_EXTERN_C_END 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric #endif /* LLVM_C_LTO_H */ 960