lto.h 29 KB

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