lto.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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 29
  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. * This function can be used by the linker to check if a given module has
  286. * any constructor or destructor functions.
  287. *
  288. * Returns true if the module has either the @llvm.global_ctors or the
  289. * @llvm.global_dtors symbol. Otherwise returns false.
  290. *
  291. * \since LTO_API_VERSION=29
  292. */
  293. extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod);
  294. /**
  295. * Diagnostic severity.
  296. *
  297. * \since LTO_API_VERSION=7
  298. */
  299. typedef enum {
  300. LTO_DS_ERROR = 0,
  301. LTO_DS_WARNING = 1,
  302. LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10.
  303. LTO_DS_NOTE = 2
  304. } lto_codegen_diagnostic_severity_t;
  305. /**
  306. * Diagnostic handler type.
  307. * \p severity defines the severity.
  308. * \p diag is the actual diagnostic.
  309. * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
  310. * \p ctxt is used to pass the context set with the diagnostic handler.
  311. *
  312. * \since LTO_API_VERSION=7
  313. */
  314. typedef void (*lto_diagnostic_handler_t)(
  315. lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
  316. /**
  317. * Set a diagnostic handler and the related context (void *).
  318. * This is more general than lto_get_error_message, as the diagnostic handler
  319. * can be called at anytime within lto.
  320. *
  321. * \since LTO_API_VERSION=7
  322. */
  323. extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
  324. lto_diagnostic_handler_t,
  325. void *);
  326. /**
  327. * Instantiates a code generator.
  328. * Returns NULL on error (check lto_get_error_message() for details).
  329. *
  330. * All modules added using \a lto_codegen_add_module() must have been created
  331. * in the same context as the codegen.
  332. *
  333. * \since prior to LTO_API_VERSION=3
  334. */
  335. extern lto_code_gen_t
  336. lto_codegen_create(void);
  337. /**
  338. * Instantiate a code generator in its own context.
  339. *
  340. * Instantiates a code generator in its own context. Modules added via \a
  341. * lto_codegen_add_module() must have all been created in the same context,
  342. * using \a lto_module_create_in_codegen_context().
  343. *
  344. * \since LTO_API_VERSION=11
  345. */
  346. extern lto_code_gen_t
  347. lto_codegen_create_in_local_context(void);
  348. /**
  349. * Frees all code generator and all memory it internally allocated.
  350. * Upon return the lto_code_gen_t is no longer valid.
  351. *
  352. * \since prior to LTO_API_VERSION=3
  353. */
  354. extern void
  355. lto_codegen_dispose(lto_code_gen_t);
  356. /**
  357. * Add an object module to the set of modules for which code will be generated.
  358. * Returns true on error (check lto_get_error_message() for details).
  359. *
  360. * \c cg and \c mod must both be in the same context. See \a
  361. * lto_codegen_create_in_local_context() and \a
  362. * lto_module_create_in_codegen_context().
  363. *
  364. * \since prior to LTO_API_VERSION=3
  365. */
  366. extern lto_bool_t
  367. lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
  368. /**
  369. * Sets the object module for code generation. This will transfer the ownership
  370. * of the module to the code generator.
  371. *
  372. * \c cg and \c mod must both be in the same context.
  373. *
  374. * \since LTO_API_VERSION=13
  375. */
  376. extern void
  377. lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod);
  378. /**
  379. * Sets if debug info should be generated.
  380. * Returns true on error (check lto_get_error_message() for details).
  381. *
  382. * \since prior to LTO_API_VERSION=3
  383. */
  384. extern lto_bool_t
  385. lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
  386. /**
  387. * Sets which PIC code model to generated.
  388. * Returns true on error (check lto_get_error_message() for details).
  389. *
  390. * \since prior to LTO_API_VERSION=3
  391. */
  392. extern lto_bool_t
  393. lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
  394. /**
  395. * Sets the cpu to generate code for.
  396. *
  397. * \since LTO_API_VERSION=4
  398. */
  399. extern void
  400. lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
  401. /**
  402. * Sets the location of the assembler tool to run. If not set, libLTO
  403. * will use gcc to invoke the assembler.
  404. *
  405. * \since LTO_API_VERSION=3
  406. */
  407. extern void
  408. lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path);
  409. /**
  410. * Sets extra arguments that libLTO should pass to the assembler.
  411. *
  412. * \since LTO_API_VERSION=4
  413. */
  414. extern void
  415. lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
  416. int nargs);
  417. /**
  418. * Adds to a list of all global symbols that must exist in the final generated
  419. * code. If a function is not listed there, it might be inlined into every usage
  420. * and optimized away.
  421. *
  422. * \since prior to LTO_API_VERSION=3
  423. */
  424. extern void
  425. lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol);
  426. /**
  427. * Writes a new object file at the specified path that contains the
  428. * merged contents of all modules added so far.
  429. * Returns true on error (check lto_get_error_message() for details).
  430. *
  431. * \since LTO_API_VERSION=5
  432. */
  433. extern lto_bool_t
  434. lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path);
  435. /**
  436. * Generates code for all added modules into one native object file.
  437. * This calls lto_codegen_optimize then lto_codegen_compile_optimized.
  438. *
  439. * On success returns a pointer to a generated mach-o/ELF buffer and
  440. * length set to the buffer size. The buffer is owned by the
  441. * lto_code_gen_t and will be freed when lto_codegen_dispose()
  442. * is called, or lto_codegen_compile() is called again.
  443. * On failure, returns NULL (check lto_get_error_message() for details).
  444. *
  445. * \since prior to LTO_API_VERSION=3
  446. */
  447. extern const void*
  448. lto_codegen_compile(lto_code_gen_t cg, size_t* length);
  449. /**
  450. * Generates code for all added modules into one native object file.
  451. * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead
  452. * of returning a generated mach-o/ELF buffer, it writes to a file).
  453. *
  454. * The name of the file is written to name. Returns true on error.
  455. *
  456. * \since LTO_API_VERSION=5
  457. */
  458. extern lto_bool_t
  459. lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
  460. /**
  461. * Runs optimization for the merged module. Returns true on error.
  462. *
  463. * \since LTO_API_VERSION=12
  464. */
  465. extern lto_bool_t
  466. lto_codegen_optimize(lto_code_gen_t cg);
  467. /**
  468. * Generates code for the optimized merged module into one native object file.
  469. * It will not run any IR optimizations on the merged module.
  470. *
  471. * On success returns a pointer to a generated mach-o/ELF buffer and length set
  472. * to the buffer size. The buffer is owned by the lto_code_gen_t and will be
  473. * freed when lto_codegen_dispose() is called, or
  474. * lto_codegen_compile_optimized() is called again. On failure, returns NULL
  475. * (check lto_get_error_message() for details).
  476. *
  477. * \since LTO_API_VERSION=12
  478. */
  479. extern const void*
  480. lto_codegen_compile_optimized(lto_code_gen_t cg, size_t* length);
  481. /**
  482. * Returns the runtime API version.
  483. *
  484. * \since LTO_API_VERSION=12
  485. */
  486. extern unsigned int
  487. lto_api_version(void);
  488. /**
  489. * Parses options immediately, making them available as early as possible. For
  490. * example during executing codegen::InitTargetOptionsFromCodeGenFlags. Since
  491. * parsing shud only happen once, only one of lto_codegen_debug_options or
  492. * lto_set_debug_options should be called.
  493. *
  494. * This function takes one or more options separated by spaces.
  495. * Warning: passing file paths through this function may confuse the argument
  496. * parser if the paths contain spaces.
  497. *
  498. * \since LTO_API_VERSION=28
  499. */
  500. extern void lto_set_debug_options(const char *const *options, int number);
  501. /**
  502. * Sets options to help debug codegen bugs. Since parsing shud only happen once,
  503. * only one of lto_codegen_debug_options or lto_set_debug_options
  504. * should be called.
  505. *
  506. * This function takes one or more options separated by spaces.
  507. * Warning: passing file paths through this function may confuse the argument
  508. * parser if the paths contain spaces.
  509. *
  510. * \since prior to LTO_API_VERSION=3
  511. */
  512. extern void
  513. lto_codegen_debug_options(lto_code_gen_t cg, const char *);
  514. /**
  515. * Same as the previous function, but takes every option separately through an
  516. * array.
  517. *
  518. * \since prior to LTO_API_VERSION=26
  519. */
  520. extern void lto_codegen_debug_options_array(lto_code_gen_t cg,
  521. const char *const *, int number);
  522. /**
  523. * Initializes LLVM disassemblers.
  524. * FIXME: This doesn't really belong here.
  525. *
  526. * \since LTO_API_VERSION=5
  527. */
  528. extern void
  529. lto_initialize_disassembler(void);
  530. /**
  531. * Sets if we should run internalize pass during optimization and code
  532. * generation.
  533. *
  534. * \since LTO_API_VERSION=14
  535. */
  536. extern void
  537. lto_codegen_set_should_internalize(lto_code_gen_t cg,
  538. lto_bool_t ShouldInternalize);
  539. /**
  540. * Set whether to embed uselists in bitcode.
  541. *
  542. * Sets whether \a lto_codegen_write_merged_modules() should embed uselists in
  543. * output bitcode. This should be turned on for all -save-temps output.
  544. *
  545. * \since LTO_API_VERSION=15
  546. */
  547. extern void
  548. lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
  549. lto_bool_t ShouldEmbedUselists);
  550. /** Opaque reference to an LTO input file */
  551. typedef struct LLVMOpaqueLTOInput *lto_input_t;
  552. /**
  553. * Creates an LTO input file from a buffer. The path
  554. * argument is used for diagnotics as this function
  555. * otherwise does not know which file the given buffer
  556. * is associated with.
  557. *
  558. * \since LTO_API_VERSION=24
  559. */
  560. extern lto_input_t lto_input_create(const void *buffer,
  561. size_t buffer_size,
  562. const char *path);
  563. /**
  564. * Frees all memory internally allocated by the LTO input file.
  565. * Upon return the lto_module_t is no longer valid.
  566. *
  567. * \since LTO_API_VERSION=24
  568. */
  569. extern void lto_input_dispose(lto_input_t input);
  570. /**
  571. * Returns the number of dependent library specifiers
  572. * for the given LTO input file.
  573. *
  574. * \since LTO_API_VERSION=24
  575. */
  576. extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input);
  577. /**
  578. * Returns the ith dependent library specifier
  579. * for the given LTO input file. The returned
  580. * string is not null-terminated.
  581. *
  582. * \since LTO_API_VERSION=24
  583. */
  584. extern const char * lto_input_get_dependent_library(lto_input_t input,
  585. size_t index,
  586. size_t *size);
  587. /**
  588. * Returns the list of libcall symbols that can be generated by LTO
  589. * that might not be visible from the symbol table of bitcode files.
  590. *
  591. * \since prior to LTO_API_VERSION=25
  592. */
  593. extern const char *const *lto_runtime_lib_symbols_list(size_t *size);
  594. /**
  595. * @} // endgoup LLVMCLTO
  596. * @defgroup LLVMCTLTO ThinLTO
  597. * @ingroup LLVMC
  598. *
  599. * @{
  600. */
  601. /**
  602. * Type to wrap a single object returned by ThinLTO.
  603. *
  604. * \since LTO_API_VERSION=18
  605. */
  606. typedef struct {
  607. const char *Buffer;
  608. size_t Size;
  609. } LTOObjectBuffer;
  610. /**
  611. * Instantiates a ThinLTO code generator.
  612. * Returns NULL on error (check lto_get_error_message() for details).
  613. *
  614. *
  615. * The ThinLTOCodeGenerator is not intended to be reuse for multiple
  616. * compilation: the model is that the client adds modules to the generator and
  617. * ask to perform the ThinLTO optimizations / codegen, and finally destroys the
  618. * codegenerator.
  619. *
  620. * \since LTO_API_VERSION=18
  621. */
  622. extern thinlto_code_gen_t thinlto_create_codegen(void);
  623. /**
  624. * Frees the generator and all memory it internally allocated.
  625. * Upon return the thinlto_code_gen_t is no longer valid.
  626. *
  627. * \since LTO_API_VERSION=18
  628. */
  629. extern void thinlto_codegen_dispose(thinlto_code_gen_t cg);
  630. /**
  631. * Add a module to a ThinLTO code generator. Identifier has to be unique among
  632. * all the modules in a code generator. The data buffer stays owned by the
  633. * client, and is expected to be available for the entire lifetime of the
  634. * thinlto_code_gen_t it is added to.
  635. *
  636. * On failure, returns NULL (check lto_get_error_message() for details).
  637. *
  638. *
  639. * \since LTO_API_VERSION=18
  640. */
  641. extern void thinlto_codegen_add_module(thinlto_code_gen_t cg,
  642. const char *identifier, const char *data,
  643. int length);
  644. /**
  645. * Optimize and codegen all the modules added to the codegenerator using
  646. * ThinLTO. Resulting objects are accessible using thinlto_module_get_object().
  647. *
  648. * \since LTO_API_VERSION=18
  649. */
  650. extern void thinlto_codegen_process(thinlto_code_gen_t cg);
  651. /**
  652. * Returns the number of object files produced by the ThinLTO CodeGenerator.
  653. *
  654. * It usually matches the number of input files, but this is not a guarantee of
  655. * the API and may change in future implementation, so the client should not
  656. * assume it.
  657. *
  658. * \since LTO_API_VERSION=18
  659. */
  660. extern unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg);
  661. /**
  662. * Returns a reference to the ith object file produced by the ThinLTO
  663. * CodeGenerator.
  664. *
  665. * Client should use \p thinlto_module_get_num_objects() to get the number of
  666. * available objects.
  667. *
  668. * \since LTO_API_VERSION=18
  669. */
  670. extern LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
  671. unsigned int index);
  672. /**
  673. * Returns the number of object files produced by the ThinLTO CodeGenerator.
  674. *
  675. * It usually matches the number of input files, but this is not a guarantee of
  676. * the API and may change in future implementation, so the client should not
  677. * assume it.
  678. *
  679. * \since LTO_API_VERSION=21
  680. */
  681. unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg);
  682. /**
  683. * Returns the path to the ith object file produced by the ThinLTO
  684. * CodeGenerator.
  685. *
  686. * Client should use \p thinlto_module_get_num_object_files() to get the number
  687. * of available objects.
  688. *
  689. * \since LTO_API_VERSION=21
  690. */
  691. const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
  692. unsigned int index);
  693. /**
  694. * Sets which PIC code model to generate.
  695. * Returns true on error (check lto_get_error_message() for details).
  696. *
  697. * \since LTO_API_VERSION=18
  698. */
  699. extern lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
  700. lto_codegen_model);
  701. /**
  702. * Sets the path to a directory to use as a storage for temporary bitcode files.
  703. * The intention is to make the bitcode files available for debugging at various
  704. * stage of the pipeline.
  705. *
  706. * \since LTO_API_VERSION=18
  707. */
  708. extern void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
  709. const char *save_temps_dir);
  710. /**
  711. * Set the path to a directory where to save generated object files. This
  712. * path can be used by a linker to request on-disk files instead of in-memory
  713. * buffers. When set, results are available through
  714. * thinlto_module_get_object_file() instead of thinlto_module_get_object().
  715. *
  716. * \since LTO_API_VERSION=21
  717. */
  718. void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
  719. const char *save_temps_dir);
  720. /**
  721. * Sets the cpu to generate code for.
  722. *
  723. * \since LTO_API_VERSION=18
  724. */
  725. extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu);
  726. /**
  727. * Disable CodeGen, only run the stages till codegen and stop. The output will
  728. * be bitcode.
  729. *
  730. * \since LTO_API_VERSION=19
  731. */
  732. extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
  733. lto_bool_t disable);
  734. /**
  735. * Perform CodeGen only: disable all other stages.
  736. *
  737. * \since LTO_API_VERSION=19
  738. */
  739. extern void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
  740. lto_bool_t codegen_only);
  741. /**
  742. * Parse -mllvm style debug options.
  743. *
  744. * \since LTO_API_VERSION=18
  745. */
  746. extern void thinlto_debug_options(const char *const *options, int number);
  747. /**
  748. * Test if a module has support for ThinLTO linking.
  749. *
  750. * \since LTO_API_VERSION=18
  751. */
  752. extern lto_bool_t lto_module_is_thinlto(lto_module_t mod);
  753. /**
  754. * Adds a symbol to the list of global symbols that must exist in the final
  755. * generated code. If a function is not listed there, it might be inlined into
  756. * every usage and optimized away. For every single module, the functions
  757. * referenced from code outside of the ThinLTO modules need to be added here.
  758. *
  759. * \since LTO_API_VERSION=18
  760. */
  761. extern void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
  762. const char *name,
  763. int length);
  764. /**
  765. * Adds a symbol to the list of global symbols that are cross-referenced between
  766. * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
  767. * references from a ThinLTO module to this symbol is optimized away, then
  768. * the symbol can be discarded.
  769. *
  770. * \since LTO_API_VERSION=18
  771. */
  772. extern void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
  773. const char *name,
  774. int length);
  775. /**
  776. * @} // endgoup LLVMCTLTO
  777. * @defgroup LLVMCTLTO_CACHING ThinLTO Cache Control
  778. * @ingroup LLVMCTLTO
  779. *
  780. * These entry points control the ThinLTO cache. The cache is intended to
  781. * support incremental builds, and thus needs to be persistent across builds.
  782. * The client enables the cache by supplying a path to an existing directory.
  783. * The code generator will use this to store objects files that may be reused
  784. * during a subsequent build.
  785. * To avoid filling the disk space, a few knobs are provided:
  786. * - The pruning interval limits the frequency at which the garbage collector
  787. * will try to scan the cache directory to prune expired entries.
  788. * Setting to a negative number disables the pruning.
  789. * - The pruning expiration time indicates to the garbage collector how old an
  790. * entry needs to be to be removed.
  791. * - Finally, the garbage collector can be instructed to prune the cache until
  792. * the occupied space goes below a threshold.
  793. * @{
  794. */
  795. /**
  796. * Sets the path to a directory to use as a cache storage for incremental build.
  797. * Setting this activates caching.
  798. *
  799. * \since LTO_API_VERSION=18
  800. */
  801. extern void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
  802. const char *cache_dir);
  803. /**
  804. * Sets the cache pruning interval (in seconds). A negative value disables the
  805. * pruning. An unspecified default value will be applied, and a value of 0 will
  806. * force prunning to occur.
  807. *
  808. * \since LTO_API_VERSION=18
  809. */
  810. extern void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
  811. int interval);
  812. /**
  813. * Sets the maximum cache size that can be persistent across build, in terms of
  814. * percentage of the available space on the disk. Set to 100 to indicate
  815. * no limit, 50 to indicate that the cache size will not be left over half the
  816. * available space. A value over 100 will be reduced to 100, a value of 0 will
  817. * be ignored. An unspecified default value will be applied.
  818. *
  819. * The formula looks like:
  820. * AvailableSpace = FreeSpace + ExistingCacheSize
  821. * NewCacheSize = AvailableSpace * P/100
  822. *
  823. * \since LTO_API_VERSION=18
  824. */
  825. extern void thinlto_codegen_set_final_cache_size_relative_to_available_space(
  826. thinlto_code_gen_t cg, unsigned percentage);
  827. /**
  828. * Sets the expiration (in seconds) for an entry in the cache. An unspecified
  829. * default value will be applied. A value of 0 will be ignored.
  830. *
  831. * \since LTO_API_VERSION=18
  832. */
  833. extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
  834. unsigned expiration);
  835. /**
  836. * Sets the maximum size of the cache directory (in bytes). A value over the
  837. * amount of available space on the disk will be reduced to the amount of
  838. * available space. An unspecified default value will be applied. A value of 0
  839. * will be ignored.
  840. *
  841. * \since LTO_API_VERSION=22
  842. */
  843. extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg,
  844. unsigned max_size_bytes);
  845. /**
  846. * Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in
  847. * megabytes (2^20 bytes).
  848. *
  849. * \since LTO_API_VERSION=23
  850. */
  851. extern void
  852. thinlto_codegen_set_cache_size_megabytes(thinlto_code_gen_t cg,
  853. unsigned max_size_megabytes);
  854. /**
  855. * Sets the maximum number of files in the cache directory. An unspecified
  856. * default value will be applied. A value of 0 will be ignored.
  857. *
  858. * \since LTO_API_VERSION=22
  859. */
  860. extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg,
  861. unsigned max_size_files);
  862. /**
  863. * @} // endgroup LLVMCTLTO_CACHING
  864. */
  865. LLVM_C_EXTERN_C_END
  866. #endif /* LLVM_C_LTO_H */
  867. #ifdef __GNUC__
  868. #pragma GCC diagnostic pop
  869. #endif