llvm_sys/
lto.rs

1//! Abstract link time optimization.
2//!
3//! ## ThinLTO
4//!
5//! ThinLTO is designed to do LTO while requiring fewer resources than regular
6//! LTO. It can run much faster and in less memory (comparable to linking
7//! without LTO) than regular LTO, with essentially no loss in optimization.
8
9#![allow(non_camel_case_types)]
10
11pub type lto_bool_t = u8;
12
13// This looks kind of like bitflags but I'm not sure.
14#[repr(C)]
15#[derive(Clone, Copy, Debug, PartialEq)]
16pub enum lto_symbol_attributes {
17    LTO_SYMBOL_ALIGNMENT_MASK = 31,
18    LTO_SYMBOL_PERMISSIONS_MASK = 224,
19    LTO_SYMBOL_PERMISSIONS_CODE = 160,
20    LTO_SYMBOL_PERMISSIONS_DATA = 192,
21    LTO_SYMBOL_PERMISSIONS_RODATA = 128,
22    LTO_SYMBOL_DEFINITION_MASK = 1792,
23    LTO_SYMBOL_DEFINITION_REGULAR = 256,
24    LTO_SYMBOL_DEFINITION_TENTATIVE = 512,
25    LTO_SYMBOL_DEFINITION_WEAK = 768,
26    LTO_SYMBOL_DEFINITION_UNDEFINED = 1024,
27    LTO_SYMBOL_DEFINITION_WEAKUNDEF = 1280,
28    LTO_SYMBOL_SCOPE_MASK = 14336,
29    LTO_SYMBOL_SCOPE_INTERNAL = 2048,
30    LTO_SYMBOL_SCOPE_HIDDEN = 0x1000,
31    LTO_SYMBOL_SCOPE_PROTECTED = 0x2000,
32    LTO_SYMBOL_SCOPE_DEFAULT = 0x1800,
33    LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x2800,
34    /// Added in LLVM 3.7.
35    LTO_SYMBOL_COMDAT = 0x4000,
36    /// Added in LLVM 3.7.
37    LTO_SYMBOL_ALIAS = 0x8000,
38}
39
40#[repr(C)]
41#[derive(Clone, Copy, Debug, PartialEq)]
42pub enum lto_debug_model {
43    LTO_DEBUG_MODEL_NONE = 0,
44    LTO_DEBUG_MODEL_DWARF = 1,
45}
46
47#[repr(C)]
48#[derive(Clone, Copy, Debug, PartialEq)]
49pub enum lto_codegen_model {
50    LTO_CODEGEN_PIC_MODEL_STATIC = 0,
51    LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1,
52    LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
53    LTO_CODEGEN_PIC_MODEL_DEFAULT = 3,
54}
55
56#[derive(Debug)]
57pub enum LLVMOpaqueLTOModule {}
58
59pub type lto_module_t = *mut LLVMOpaqueLTOModule;
60
61#[derive(Debug)]
62pub enum LLVMOpaqueLTOCodeGenerator {}
63
64pub type lto_code_gen_t = *mut LLVMOpaqueLTOCodeGenerator;
65
66#[derive(Debug)]
67pub enum LLVMOpaqueThinLTOCodeGenerator {}
68
69pub type thinlto_code_gen_t = *mut LLVMOpaqueThinLTOCodeGenerator;
70
71#[derive(Debug)]
72pub enum LLVMOpaqueLTOInput {}
73
74pub type lto_input_t = *mut LLVMOpaqueLTOInput;
75
76#[repr(C)]
77#[derive(Clone, Copy, Debug, PartialEq)]
78pub enum lto_codegen_diagnostic_severity_t {
79    LTO_DS_ERROR = 0,
80    LTO_DS_WARNING = 1,
81    LTO_DS_REMARK = 3,
82    LTO_DS_NOTE = 2,
83}
84
85pub type lto_diagnostic_handler_t = Option<
86    extern "C" fn(
87        severity: lto_codegen_diagnostic_severity_t,
88        diag: *const ::libc::c_char,
89        ctxt: *mut ::libc::c_void,
90    ),
91>;
92
93extern "C" {
94    pub fn lto_get_version() -> *const ::libc::c_char;
95    pub fn lto_get_error_message() -> *const ::libc::c_char;
96    pub fn lto_module_is_object_file(path: *const ::libc::c_char) -> lto_bool_t;
97    pub fn lto_module_is_object_file_for_target(
98        path: *const ::libc::c_char,
99        target_triple_prefix: *const ::libc::c_char,
100    ) -> lto_bool_t;
101    /// Return true if `Buffer` contains a bitcode file with ObjC code
102    /// (category or class) in it.
103    pub fn lto_module_has_objc_category(
104        mem: *const ::libc::c_void,
105        length: ::libc::size_t,
106    ) -> lto_bool_t;
107    /// Checks if a buffer is a loadable object file.
108    pub fn lto_module_is_object_file_in_memory(
109        mem: *const ::libc::c_void,
110        length: ::libc::size_t,
111    ) -> lto_bool_t;
112    pub fn lto_module_is_object_file_in_memory_for_target(
113        mem: *const ::libc::c_void,
114        length: ::libc::size_t,
115        target_triple_prefix: *const ::libc::c_char,
116    ) -> lto_bool_t;
117    pub fn lto_module_create(path: *const ::libc::c_char) -> lto_module_t;
118    pub fn lto_module_create_from_memory(
119        mem: *const ::libc::c_void,
120        length: ::libc::size_t,
121    ) -> lto_module_t;
122    pub fn lto_module_create_from_memory_with_path(
123        mem: *const ::libc::c_void,
124        length: ::libc::size_t,
125        path: *const ::libc::c_char,
126    ) -> lto_module_t;
127    pub fn lto_module_create_in_local_context(
128        mem: *const ::libc::c_void,
129        length: ::libc::size_t,
130        path: *const ::libc::c_char,
131    ) -> lto_module_t;
132    pub fn lto_module_create_in_codegen_context(
133        mem: *const ::libc::c_void,
134        length: ::libc::size_t,
135        path: *const ::libc::c_char,
136        cg: lto_code_gen_t,
137    ) -> lto_module_t;
138    pub fn lto_module_create_from_fd(
139        fd: ::libc::c_int,
140        path: *const ::libc::c_char,
141        file_size: ::libc::size_t,
142    ) -> lto_module_t;
143    pub fn lto_module_create_from_fd_at_offset(
144        fd: ::libc::c_int,
145        path: *const ::libc::c_char,
146        file_size: ::libc::size_t,
147        map_size: ::libc::size_t,
148        offset: ::libc::off_t,
149    ) -> lto_module_t;
150    pub fn lto_module_dispose(_mod: lto_module_t);
151    pub fn lto_module_get_target_triple(_mod: lto_module_t) -> *const ::libc::c_char;
152    pub fn lto_module_set_target_triple(_mod: lto_module_t, triple: *const ::libc::c_char);
153    pub fn lto_module_get_num_symbols(_mod: lto_module_t) -> ::libc::c_uint;
154    pub fn lto_module_get_symbol_name(
155        _mod: lto_module_t,
156        index: ::libc::c_uint,
157    ) -> *const ::libc::c_char;
158    pub fn lto_module_get_symbol_attribute(
159        _mod: lto_module_t,
160        index: ::libc::c_uint,
161    ) -> lto_symbol_attributes;
162    /// Returns the number of asm undefined symbols in the object module.
163    ///
164    /// Available since before LTO API version 30.
165    pub fn lto_module_get_num_asm_undef_symbols(mod_: lto_module_t) -> ::libc::c_uint;
166    /// Returns the name of the ith asm undefined symbol in the object module.
167    ///
168    /// Available since before LTO API version 30.
169    pub fn lto_module_get_asm_undef_symbol_name(
170        mod_: lto_module_t,
171        index: ::libc::c_uint,
172    ) -> *const ::libc::c_char;
173    /// Returns the module's linker options.
174    ///
175    /// The linker options may consist of multiple flags. It is the linker's
176    /// responsibility to split the flags using a platform-specific mechanism.
177    ///
178    /// Added in LLVM 3.7.
179    pub fn lto_module_get_linkeropts(_mod: lto_module_t) -> *const ::libc::c_char;
180    pub fn lto_module_get_macho_cputype(
181        _mod: lto_module_t,
182        out_cputype: *mut ::libc::c_uint,
183        out_cpusubtype: *mut ::libc::c_uint,
184    ) -> lto_bool_t;
185    /// Return true if the module has either the `@llvm.global_ctors` or the `@llvm.global_dtors`
186    /// symbol.
187    ///
188    /// Added in API version 29 (LLVM 14).
189    pub fn lto_module_has_ctor_dtor(mod_: lto_module_t) -> lto_bool_t;
190    pub fn lto_codegen_set_diagnostic_handler(
191        arg1: lto_code_gen_t,
192        arg2: lto_diagnostic_handler_t,
193        arg3: *mut ::libc::c_void,
194    );
195    pub fn lto_codegen_create() -> lto_code_gen_t;
196    pub fn lto_codegen_create_in_local_context() -> lto_code_gen_t;
197    pub fn lto_codegen_dispose(arg1: lto_code_gen_t);
198    pub fn lto_codegen_add_module(cg: lto_code_gen_t, _mod: lto_module_t) -> lto_bool_t;
199    /// Sets the object module for code gneeration. This will transfer ownership
200    /// of the module to the code generator.
201    ///
202    /// Added in LLVM 3.7.
203    pub fn lto_codegen_set_module(cg: lto_code_gen_t, _mod: lto_module_t);
204    pub fn lto_codegen_set_debug_model(cg: lto_code_gen_t, arg1: lto_debug_model) -> lto_bool_t;
205    pub fn lto_codegen_set_pic_model(cg: lto_code_gen_t, arg1: lto_codegen_model) -> lto_bool_t;
206    pub fn lto_codegen_set_cpu(cg: lto_code_gen_t, cpu: *const ::libc::c_char);
207    pub fn lto_codegen_set_assembler_path(cg: lto_code_gen_t, path: *const ::libc::c_char);
208    pub fn lto_codegen_set_assembler_args(
209        cg: lto_code_gen_t,
210        args: *mut *const ::libc::c_char,
211        nargs: ::libc::c_int,
212    );
213    pub fn lto_codegen_add_must_preserve_symbol(cg: lto_code_gen_t, symbol: *const ::libc::c_char);
214    pub fn lto_codegen_write_merged_modules(
215        cg: lto_code_gen_t,
216        path: *const ::libc::c_char,
217    ) -> lto_bool_t;
218    pub fn lto_codegen_compile(
219        cg: lto_code_gen_t,
220        length: *mut ::libc::size_t,
221    ) -> *const ::libc::c_void;
222    pub fn lto_codegen_compile_to_file(
223        cg: lto_code_gen_t,
224        name: *mut *const ::libc::c_char,
225    ) -> lto_bool_t;
226    /// Runs optimization for the merged module.
227    ///
228    /// Returns true on error.
229    ///
230    /// Added in LLVM 3.7.
231    pub fn lto_codegen_optimize(cg: lto_code_gen_t) -> lto_bool_t;
232    /// Generates code for the optimized merged module into one native object file.
233    ///
234    /// Does not run IR optimizations on the merged module.
235    ///
236    /// Returns a pointer to the generated mach-o/ELF buffer with length
237    /// set to the buffer size. This buffer is owned by `cg` and will be
238    /// freed when `lto_codegen_dispose` is called or `lto_codegen_compile_optimized`
239    /// is called again. Returns null on failure.
240    ///
241    /// Added in LLVM 3.7.
242    pub fn lto_codegen_compile_optimized(
243        cg: lto_code_gen_t,
244        length: *mut ::libc::size_t,
245    ) -> *mut ::libc::c_void;
246    /// Returns the runtime API version.
247    ///
248    /// Added in LLVM 3.7.
249    pub fn lto_api_version() -> ::libc::c_uint;
250    pub fn lto_set_debug_options(options: *mut *const ::libc::c_char, number: ::libc::c_int);
251    pub fn lto_codegen_debug_options(cg: lto_code_gen_t, arg1: *const ::libc::c_char);
252    pub fn lto_codegen_debug_options_array(
253        cg: lto_code_gen_t,
254        arg2: *const *const ::libc::c_char,
255        number: ::libc::c_int,
256    );
257    pub fn lto_initialize_disassembler();
258    /// Sets if we should run the internalize pass during optimization and code generation.
259    ///
260    /// Added in LLVM 3.7.
261    pub fn lto_codegen_set_should_internalize(cg: lto_code_gen_t, ShouldInternalize: lto_bool_t);
262    /// Set whether to embed uselists in bitcode.
263    ///
264    /// Sets whether `lto_codegen_write_merged_modules` should embed uselists in
265    /// output bitcode. This should be turned on for all -save-temps output.
266    ///
267    /// Added in LLVM 3.7.
268    pub fn lto_codegen_set_should_embed_uselists(
269        cg: lto_code_gen_t,
270        ShouldEmbedUselists: lto_bool_t,
271    );
272}
273
274/// Type to wrap a single object returned by ThinLTO.
275#[repr(C)]
276#[derive(Debug)]
277#[allow(non_snake_case)]
278pub struct LTOObjectBuffer {
279    Buffer: *const ::libc::c_char,
280    Size: ::libc::size_t,
281}
282
283extern "C" {
284    /// Instantiates a ThinLTO code generator.
285    ///
286    /// Returns null on error (check `lto_get_error_message` for details).
287    ///
288    /// The code generator should not be reused.
289    pub fn thinlto_create_codegen() -> thinlto_code_gen_t;
290    /// Frees a code generator.
291    pub fn thinlto_codegen_dispose(cg: thinlto_code_gen_t);
292    /// Add a module to a code generator.
293    ///
294    /// Identifier must be unique among all the modules in the code generator.
295    /// The data buffer remains owned by the client, and must live at least
296    /// as long as the code generator.
297    ///
298    /// Returns null on failure.
299    pub fn thinlto_codegen_add_module(
300        cg: thinlto_code_gen_t,
301        identifier: *const ::libc::c_char,
302        data: *const ::libc::c_char,
303        length: ::libc::c_int,
304    );
305    /// Optimize and codegen all modules attached to the code generator.
306    ///
307    /// Resulting objects are accessible with `thinlto_module_get_object`.
308    pub fn thinlto_codegen_process(cg: thinlto_code_gen_t);
309    /// Return the number of object files produced by the code generator.
310    ///
311    /// This usually matches the number of input files, but is not guaranteed
312    /// to.
313    pub fn thinlto_module_get_num_objects(cg: thinlto_code_gen_t) -> ::libc::c_int;
314    /// Return a reference to the `index`th object file produced by the
315    /// code generator.
316    pub fn thinlto_module_get_object(
317        cg: thinlto_code_gen_t,
318        index: ::libc::c_uint,
319    ) -> LTOObjectBuffer;
320    /// Return the number of object files produced by the code generator.
321    ///
322    /// Usually the same as the number of input files, but not guaranteed.
323    pub fn thinlto_module_get_num_object_files(cg: thinlto_code_gen_t) -> ::libc::c_uint;
324    /// Return the path to the ith output object file.
325    ///
326    /// Use `thinlto_module_get_num_object_files` to get the number of available objects.
327    pub fn thinlto_module_get_object_file(
328        cg: thinlto_code_gen_t,
329        index: ::libc::c_uint,
330    ) -> *const ::libc::c_char;
331    /// Set which PIC code model to generate.
332    ///
333    /// Returns true on error.
334    pub fn thinlto_codegen_set_pic_model(
335        cg: thinlto_code_gen_t,
336        model: lto_codegen_model,
337    ) -> lto_bool_t;
338
339    // ThinLTO cache control.
340    // Set the path to a directory to use as cache for increment build.
341    //
342    // Setting this activates caching.
343    pub fn thinlto_codegen_set_cache_dir(cg: thinlto_code_gen_t, cache_dir: *const ::libc::c_char);
344    /// Set the cache pruning interval, in seconds.
345    ///
346    /// A negative value disables pruning, and 0 will force pruning to occur.
347    pub fn thinlto_codegen_set_cache_pruning_interval(
348        cg: thinlto_code_gen_t,
349        interval: ::libc::c_int,
350    );
351    /// Set the maximum cache size to persist across builds.
352    ///
353    /// This is expressed as a percentage of available disk space. 100 means no limit,
354    /// and 50 means no more than half of the available disk space. 0 is ignored, and
355    /// values over 100 will be reduced to 100.
356    pub fn thinlto_codegen_set_final_cache_size_relative_to_available_space(
357        cg: thinlto_code_gen_t,
358        percentage: ::libc::c_uint,
359    );
360    /// Set the expiration (in seconds) for cache entries.
361    pub fn thinlto_codegen_set_cache_entry_expiration(
362        cg: thinlto_code_gen_t,
363        expiration: ::libc::c_uint,
364    );
365    /// Set the maximum size of the cache directory (in bytes). A value over the
366    /// amount of available space on the disk will be reduced to the amount of
367    /// available space. An unspecified default value will be applied. A value of 0
368    /// will be ignored.
369    pub fn thinlto_codegen_set_cache_size_bytes(
370        cg: thinlto_code_gen_t,
371        max_size_bytes: ::libc::c_uint,
372    );
373    /// Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in
374    /// megabytes (2^20 bytes).
375    pub fn thinlto_codegen_set_cache_size_megabytes(
376        cg: thinlto_code_gen_t,
377        max_size_megabytes: ::libc::c_uint,
378    );
379    /// Sets the maximum number of files in the cache directory. An unspecified default value will be applied. A value of 0 will be ignored.
380    pub fn thinlto_codegen_set_cache_size_files(
381        cg: thinlto_code_gen_t,
382        max_size_files: ::libc::c_uint,
383    );
384
385    /// Create an LTO input file from a buffer.
386    pub fn lto_input_create(
387        buffer: *const ::libc::c_void,
388        buffer_size: ::libc::size_t,
389        path: *const ::libc::c_char,
390    ) -> lto_input_t;
391    /// Free all memory allocated by the input file.
392    pub fn lto_input_dispose(input: lto_input_t);
393    /// Get the number of dependent library specifiers for the given input.
394    pub fn lto_input_get_num_dependent_libraries(input: lto_input_t) -> ::libc::c_uint;
395    /// Get the `i`th dependent library specifier for the given input file.
396    ///
397    /// The returned string is not null-terminated.
398    pub fn lto_input_get_dependent_library(
399        input: lto_input_t,
400        index: ::libc::size_t,
401        size: *mut ::libc::size_t,
402    ) -> *const ::libc::c_char;
403    /// Return the list of libcall symbols that can be generated by LTO
404    /// that might not be visible from the symbol table of bitcode files.
405    pub fn lto_runtime_lib_symbols_list(size: *mut usize) -> *const *const ::libc::c_char;
406
407    /// Set the path to a directory to use as temporary bitcode storage.
408    ///
409    /// This is meant to make the bitcode files available for debugging.
410    pub fn thinlto_codegen_set_savetemps_dir(
411        cg: thinlto_code_gen_t,
412        save_temps_dir: *const ::libc::c_char,
413    );
414    /// Set the path to a directory to save generated object files.
415    ///
416    /// Set this to request on-disk rather than in-memory buffers. When set, use
417    /// `thinlto_module_get_object_file` instead of `thinlto_module_get_object`.
418    pub fn thinlto_set_generated_objects_dir(
419        cg: thinlto_code_gen_t,
420        save_temps_dir: *const ::libc::c_char,
421    );
422    /// Set the CPU to generate code for.
423    pub fn thinlto_codegen_set_cpu(cg: thinlto_code_gen_t, cpu: *const ::libc::c_char);
424    /// Disable code generation (running all stages until codegen).
425    ///
426    /// The output with codegen disabled is bitcode.
427    pub fn thinlto_codegen_disable_codegen(cg: thinlto_code_gen_t, disable: lto_bool_t);
428    /// Perform codegen only; disable all other stages.
429    pub fn thinlto_codegen_set_codegen_only(cg: thinlto_code_gen_t, codegen_only: lto_bool_t);
430    /// Parse -mllvm style debug options.
431    pub fn thinlto_debug_options(options: *const *const ::libc::c_char, number: ::libc::c_int);
432    /// Test if a module has ThinLTO linking support.
433    pub fn lto_module_is_thinlto(module: lto_module_t) -> lto_bool_t;
434    /// Add a symbol to the list of global symbols that must exist in the
435    /// final generated code.
436    ///
437    /// Functions not listed may be inlined in every usage and optimized away.
438    pub fn thinlto_codegen_add_must_preserve_symbol(
439        cg: thinlto_code_gen_t,
440        name: *const ::libc::c_char,
441        length: ::libc::c_int,
442    );
443    /// Add a symbol to the list of global symbols that are cross-referenced
444    /// between ThinLTO files.
445    ///
446    /// Symbols listed can be discarded if every reference from a ThinLTO module
447    /// to a symbol is optimized away, then the symbol can be discarded.
448    pub fn thinlto_codegen_add_cross_referenced_symbol(
449        cg: thinlto_code_gen_t,
450        name: *const ::libc::c_char,
451        length: ::libc::c_int,
452    );
453}