aot_llvm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_LLVM_H_
  6. #define _AOT_LLVM_H_
  7. #include "aot.h"
  8. #include "llvm/Config/llvm-config.h"
  9. #include "llvm-c/Types.h"
  10. #include "llvm-c/Target.h"
  11. #include "llvm-c/Core.h"
  12. #include "llvm-c/Object.h"
  13. #include "llvm-c/OrcEE.h"
  14. #include "llvm-c/ExecutionEngine.h"
  15. #include "llvm-c/Analysis.h"
  16. #include "llvm-c/BitWriter.h"
  17. #if LLVM_VERSION_MAJOR < 17
  18. #include "llvm-c/Transforms/Utils.h"
  19. #include "llvm-c/Transforms/Scalar.h"
  20. #include "llvm-c/Transforms/Vectorize.h"
  21. #include "llvm-c/Transforms/PassManagerBuilder.h"
  22. #include "llvm-c/Initialization.h"
  23. #endif
  24. #include "llvm-c/Orc.h"
  25. #include "llvm-c/Error.h"
  26. #include "llvm-c/Support.h"
  27. #include "llvm-c/TargetMachine.h"
  28. #include "llvm-c/LLJIT.h"
  29. #if WASM_ENABLE_DEBUG_AOT != 0
  30. #include "llvm-c/DebugInfo.h"
  31. #endif
  32. #include "aot_orc_extra.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #if LLVM_VERSION_MAJOR < 14
  37. #define LLVMBuildLoad2(builder, type, value, name) \
  38. LLVMBuildLoad(builder, value, name)
  39. #define LLVMBuildCall2(builder, type, func, args, num_args, name) \
  40. LLVMBuildCall(builder, func, args, num_args, name)
  41. #define LLVMBuildInBoundsGEP2(builder, type, ptr, indices, num_indices, name) \
  42. LLVMBuildInBoundsGEP(builder, ptr, indices, num_indices, name)
  43. #else
  44. /* Opaque pointer type */
  45. #define OPQ_PTR_TYPE INT8_PTR_TYPE
  46. #endif
  47. #ifndef NDEBUG
  48. #undef DEBUG_PASS
  49. #undef DUMP_MODULE
  50. // #define DEBUG_PASS
  51. // #define DUMP_MODULE
  52. #else
  53. #undef DEBUG_PASS
  54. #undef DUMP_MODULE
  55. #endif
  56. /**
  57. * Value in the WASM operation stack, each stack element
  58. * is an LLVM value
  59. */
  60. typedef struct AOTValue {
  61. struct AOTValue *next;
  62. struct AOTValue *prev;
  63. LLVMValueRef value;
  64. /* VALUE_TYPE_I32/I64/F32/F64/VOID */
  65. uint8 type;
  66. bool is_local;
  67. uint32 local_idx;
  68. } AOTValue;
  69. /**
  70. * Value stack, represents stack elements in a WASM block
  71. */
  72. typedef struct AOTValueStack {
  73. AOTValue *value_list_head;
  74. AOTValue *value_list_end;
  75. } AOTValueStack;
  76. typedef struct AOTBlock {
  77. struct AOTBlock *next;
  78. struct AOTBlock *prev;
  79. /* Block index */
  80. uint32 block_index;
  81. /* LABEL_TYPE_BLOCK/LOOP/IF/FUNCTION */
  82. uint32 label_type;
  83. /* Whether it is reachable */
  84. bool is_reachable;
  85. /* Whether skip translation of wasm else branch */
  86. bool skip_wasm_code_else;
  87. /* code of else opcode of this block, if it is a IF block */
  88. uint8 *wasm_code_else;
  89. /* code end of this block */
  90. uint8 *wasm_code_end;
  91. /* LLVM label points to code begin */
  92. LLVMBasicBlockRef llvm_entry_block;
  93. /* LLVM label points to code else */
  94. LLVMBasicBlockRef llvm_else_block;
  95. /* LLVM label points to code end */
  96. LLVMBasicBlockRef llvm_end_block;
  97. /* WASM operation stack */
  98. AOTValueStack value_stack;
  99. /* Param count/types/PHIs of this block */
  100. uint32 param_count;
  101. uint8 *param_types;
  102. LLVMValueRef *param_phis;
  103. LLVMValueRef *else_param_phis;
  104. /* Result count/types/PHIs of this block */
  105. uint32 result_count;
  106. uint8 *result_types;
  107. LLVMValueRef *result_phis;
  108. } AOTBlock;
  109. /**
  110. * Block stack, represents WASM block stack elements
  111. */
  112. typedef struct AOTBlockStack {
  113. AOTBlock *block_list_head;
  114. AOTBlock *block_list_end;
  115. /* Current block index of each block type */
  116. uint32 block_index[3];
  117. } AOTBlockStack;
  118. typedef struct AOTCheckedAddr {
  119. struct AOTCheckedAddr *next;
  120. uint32 local_idx;
  121. uint32 offset;
  122. uint32 bytes;
  123. } AOTCheckedAddr, *AOTCheckedAddrList;
  124. typedef struct AOTMemInfo {
  125. LLVMValueRef mem_base_addr;
  126. LLVMValueRef mem_data_size_addr;
  127. LLVMValueRef mem_cur_page_count_addr;
  128. LLVMValueRef mem_bound_check_1byte;
  129. LLVMValueRef mem_bound_check_2bytes;
  130. LLVMValueRef mem_bound_check_4bytes;
  131. LLVMValueRef mem_bound_check_8bytes;
  132. LLVMValueRef mem_bound_check_16bytes;
  133. } AOTMemInfo;
  134. typedef struct AOTFuncContext {
  135. AOTFunc *aot_func;
  136. LLVMValueRef func;
  137. LLVMValueRef precheck_func;
  138. LLVMTypeRef func_type;
  139. LLVMModuleRef module;
  140. AOTBlockStack block_stack;
  141. LLVMValueRef exec_env;
  142. LLVMValueRef aot_inst;
  143. LLVMValueRef argv_buf;
  144. LLVMValueRef native_stack_bound;
  145. LLVMValueRef native_stack_top_min_addr;
  146. LLVMValueRef aux_stack_bound;
  147. LLVMValueRef aux_stack_bottom;
  148. LLVMValueRef native_symbol;
  149. LLVMValueRef func_ptrs;
  150. AOTMemInfo *mem_info;
  151. LLVMValueRef cur_exception;
  152. bool mem_space_unchanged;
  153. AOTCheckedAddrList checked_addr_list;
  154. LLVMBasicBlockRef got_exception_block;
  155. LLVMBasicBlockRef func_return_block;
  156. LLVMValueRef exception_id_phi;
  157. LLVMValueRef func_type_indexes;
  158. #if WASM_ENABLE_DEBUG_AOT != 0
  159. LLVMMetadataRef debug_func;
  160. #endif
  161. unsigned int stack_consumption_for_func_call;
  162. LLVMValueRef locals[1];
  163. } AOTFuncContext;
  164. typedef struct AOTLLVMTypes {
  165. LLVMTypeRef int1_type;
  166. LLVMTypeRef int8_type;
  167. LLVMTypeRef int16_type;
  168. LLVMTypeRef int32_type;
  169. LLVMTypeRef int64_type;
  170. LLVMTypeRef float32_type;
  171. LLVMTypeRef float64_type;
  172. LLVMTypeRef void_type;
  173. LLVMTypeRef int8_ptr_type;
  174. LLVMTypeRef int8_pptr_type;
  175. LLVMTypeRef int16_ptr_type;
  176. LLVMTypeRef int32_ptr_type;
  177. LLVMTypeRef int64_ptr_type;
  178. LLVMTypeRef float32_ptr_type;
  179. LLVMTypeRef float64_ptr_type;
  180. LLVMTypeRef v128_type;
  181. LLVMTypeRef v128_ptr_type;
  182. LLVMTypeRef i8x16_vec_type;
  183. LLVMTypeRef i16x8_vec_type;
  184. LLVMTypeRef i32x4_vec_type;
  185. LLVMTypeRef i64x2_vec_type;
  186. LLVMTypeRef f32x4_vec_type;
  187. LLVMTypeRef f64x2_vec_type;
  188. LLVMTypeRef int8_ptr_type_gs;
  189. LLVMTypeRef int16_ptr_type_gs;
  190. LLVMTypeRef int32_ptr_type_gs;
  191. LLVMTypeRef int64_ptr_type_gs;
  192. LLVMTypeRef float32_ptr_type_gs;
  193. LLVMTypeRef float64_ptr_type_gs;
  194. LLVMTypeRef v128_ptr_type_gs;
  195. LLVMTypeRef i1x2_vec_type;
  196. LLVMTypeRef meta_data_type;
  197. LLVMTypeRef funcref_type;
  198. LLVMTypeRef externref_type;
  199. } AOTLLVMTypes;
  200. typedef struct AOTLLVMConsts {
  201. LLVMValueRef i1_zero;
  202. LLVMValueRef i1_one;
  203. LLVMValueRef i8_zero;
  204. LLVMValueRef i32_zero;
  205. LLVMValueRef i64_zero;
  206. LLVMValueRef f32_zero;
  207. LLVMValueRef f64_zero;
  208. LLVMValueRef i32_one;
  209. LLVMValueRef i32_two;
  210. LLVMValueRef i32_three;
  211. LLVMValueRef i32_four;
  212. LLVMValueRef i32_five;
  213. LLVMValueRef i32_six;
  214. LLVMValueRef i32_seven;
  215. LLVMValueRef i32_eight;
  216. LLVMValueRef i32_nine;
  217. LLVMValueRef i32_ten;
  218. LLVMValueRef i32_eleven;
  219. LLVMValueRef i32_twelve;
  220. LLVMValueRef i32_thirteen;
  221. LLVMValueRef i32_fourteen;
  222. LLVMValueRef i32_fifteen;
  223. LLVMValueRef i32_neg_one;
  224. LLVMValueRef i64_neg_one;
  225. LLVMValueRef i32_min;
  226. LLVMValueRef i64_min;
  227. LLVMValueRef i32_31;
  228. LLVMValueRef i32_32;
  229. LLVMValueRef i64_63;
  230. LLVMValueRef i64_64;
  231. LLVMValueRef i8x16_vec_zero;
  232. LLVMValueRef i16x8_vec_zero;
  233. LLVMValueRef i32x4_vec_zero;
  234. LLVMValueRef i64x2_vec_zero;
  235. LLVMValueRef f32x4_vec_zero;
  236. LLVMValueRef f64x2_vec_zero;
  237. LLVMValueRef i8x16_undef;
  238. LLVMValueRef i16x8_undef;
  239. LLVMValueRef i32x4_undef;
  240. LLVMValueRef i64x2_undef;
  241. LLVMValueRef f32x4_undef;
  242. LLVMValueRef f64x2_undef;
  243. LLVMValueRef i32x16_zero;
  244. LLVMValueRef i32x8_zero;
  245. LLVMValueRef i32x4_zero;
  246. LLVMValueRef i32x2_zero;
  247. } AOTLLVMConsts;
  248. /**
  249. * Compiler context
  250. */
  251. typedef struct AOTCompContext {
  252. const AOTCompData *comp_data;
  253. /* LLVM variables required to emit LLVM IR */
  254. LLVMContextRef context;
  255. LLVMBuilderRef builder;
  256. #if WASM_ENABLE_DEBUG_AOT
  257. LLVMDIBuilderRef debug_builder;
  258. LLVMMetadataRef debug_file;
  259. LLVMMetadataRef debug_comp_unit;
  260. #endif
  261. LLVMTargetMachineRef target_machine;
  262. char *target_cpu;
  263. char target_arch[16];
  264. unsigned pointer_size;
  265. /* Hardware intrinsic compability flags */
  266. uint64 flags[8];
  267. /* required by JIT */
  268. LLVMOrcLLLazyJITRef orc_jit;
  269. LLVMOrcThreadSafeContextRef orc_thread_safe_context;
  270. LLVMModuleRef module;
  271. bool is_jit_mode;
  272. /* AOT indirect mode flag & symbol list */
  273. bool is_indirect_mode;
  274. bh_list native_symbols;
  275. /* Bulk memory feature */
  276. bool enable_bulk_memory;
  277. /* Boundary Check */
  278. bool enable_bound_check;
  279. /* Native stack boundary Check */
  280. bool enable_stack_bound_check;
  281. /* Native stack usage estimation */
  282. bool enable_stack_estimation;
  283. /* 128-bit SIMD */
  284. bool enable_simd;
  285. /* Auxiliary stack overflow/underflow check */
  286. bool enable_aux_stack_check;
  287. /* Generate auxiliary stack frame */
  288. bool enable_aux_stack_frame;
  289. /* Thread Manager */
  290. bool enable_thread_mgr;
  291. /* Tail Call */
  292. bool enable_tail_call;
  293. /* Reference Types */
  294. bool enable_ref_types;
  295. /* Disable LLVM built-in intrinsics */
  296. bool disable_llvm_intrinsics;
  297. /* Disable LLVM link time optimization */
  298. bool disable_llvm_lto;
  299. /* Enable LLVM PGO (Profile-Guided Optimization) */
  300. bool enable_llvm_pgo;
  301. /* Treat unknown import function as wasm-c-api import function
  302. and allow to directly invoke it from AOT/JIT code */
  303. bool quick_invoke_c_api_import;
  304. /* Use profile file collected by LLVM PGO */
  305. char *use_prof_file;
  306. /* Enable to use segument register as the base addr
  307. of linear memory for load/store operations */
  308. bool enable_segue_i32_load;
  309. bool enable_segue_i64_load;
  310. bool enable_segue_f32_load;
  311. bool enable_segue_f64_load;
  312. bool enable_segue_v128_load;
  313. bool enable_segue_i32_store;
  314. bool enable_segue_i64_store;
  315. bool enable_segue_f32_store;
  316. bool enable_segue_f64_store;
  317. bool enable_segue_v128_store;
  318. /* Whether optimize the JITed code */
  319. bool optimize;
  320. uint32 opt_level;
  321. uint32 size_level;
  322. /* LLVM floating-point rounding mode metadata */
  323. LLVMValueRef fp_rounding_mode;
  324. /* LLVM floating-point exception behavior metadata */
  325. LLVMValueRef fp_exception_behavior;
  326. /* a global array to store stack sizes */
  327. LLVMTypeRef stack_sizes_type;
  328. LLVMValueRef stack_sizes;
  329. uint32 *jit_stack_sizes; /* for JIT */
  330. /* LLVM data types */
  331. AOTLLVMTypes basic_types;
  332. LLVMTypeRef exec_env_type;
  333. LLVMTypeRef aot_inst_type;
  334. /* LLVM const values */
  335. AOTLLVMConsts llvm_consts;
  336. /* Function contexts */
  337. /* TODO: */
  338. AOTFuncContext **func_ctxes;
  339. uint32 func_ctx_count;
  340. char **custom_sections_wp;
  341. uint32 custom_sections_count;
  342. /* 3rd-party toolchains */
  343. /* External llc compiler, if specified, wamrc will emit the llvm-ir file and
  344. * invoke the llc compiler to generate object file.
  345. * This can be used when we want to benefit from the optimization of other
  346. * LLVM based toolchains */
  347. const char *external_llc_compiler;
  348. const char *llc_compiler_flags;
  349. /* External asm compiler, if specified, wamrc will emit the text-based
  350. * assembly file (.s) and invoke the llc compiler to generate object file.
  351. * This will be useful when the upstream LLVM doesn't support to emit object
  352. * file for some architecture (such as arc) */
  353. const char *external_asm_compiler;
  354. const char *asm_compiler_flags;
  355. const char *stack_usage_file;
  356. char stack_usage_temp_file[64];
  357. const char *llvm_passes;
  358. const char *builtin_intrinsics;
  359. bool emit_frame_pointer;
  360. } AOTCompContext;
  361. enum {
  362. AOT_FORMAT_FILE,
  363. AOT_OBJECT_FILE,
  364. AOT_LLVMIR_UNOPT_FILE,
  365. AOT_LLVMIR_OPT_FILE,
  366. };
  367. /* always sync it with AOTCompOption in aot_export.h */
  368. typedef struct AOTCompOption {
  369. bool is_jit_mode;
  370. bool is_indirect_mode;
  371. char *target_arch;
  372. char *target_abi;
  373. char *target_cpu;
  374. char *cpu_features;
  375. bool is_sgx_platform;
  376. bool enable_bulk_memory;
  377. bool enable_thread_mgr;
  378. bool enable_tail_call;
  379. bool enable_simd;
  380. bool enable_ref_types;
  381. bool enable_aux_stack_check;
  382. bool enable_aux_stack_frame;
  383. bool disable_llvm_intrinsics;
  384. bool disable_llvm_lto;
  385. bool enable_llvm_pgo;
  386. bool enable_stack_estimation;
  387. bool quick_invoke_c_api_import;
  388. char *use_prof_file;
  389. uint32 opt_level;
  390. uint32 size_level;
  391. uint32 output_format;
  392. uint32 bounds_checks;
  393. uint32 stack_bounds_checks;
  394. uint32 segue_flags;
  395. char **custom_sections;
  396. uint32 custom_sections_count;
  397. const char *stack_usage_file;
  398. const char *llvm_passes;
  399. const char *builtin_intrinsics;
  400. } AOTCompOption, *aot_comp_option_t;
  401. bool
  402. aot_compiler_init(void);
  403. void
  404. aot_compiler_destroy(void);
  405. AOTCompContext *
  406. aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option);
  407. void
  408. aot_destroy_comp_context(AOTCompContext *comp_ctx);
  409. int32
  410. aot_get_native_symbol_index(AOTCompContext *comp_ctx, const char *symbol);
  411. bool
  412. aot_compile_wasm(AOTCompContext *comp_ctx);
  413. uint8 *
  414. aot_emit_elf_file(AOTCompContext *comp_ctx, uint32 *p_elf_file_size);
  415. void
  416. aot_destroy_elf_file(uint8 *elf_file);
  417. void
  418. aot_value_stack_push(AOTValueStack *stack, AOTValue *value);
  419. AOTValue *
  420. aot_value_stack_pop(AOTValueStack *stack);
  421. void
  422. aot_value_stack_destroy(AOTValueStack *stack);
  423. void
  424. aot_block_stack_push(AOTBlockStack *stack, AOTBlock *block);
  425. AOTBlock *
  426. aot_block_stack_pop(AOTBlockStack *stack);
  427. void
  428. aot_block_stack_destroy(AOTBlockStack *stack);
  429. void
  430. aot_block_destroy(AOTBlock *block);
  431. LLVMTypeRef
  432. wasm_type_to_llvm_type(const AOTLLVMTypes *llvm_types, uint8 wasm_type);
  433. bool
  434. aot_checked_addr_list_add(AOTFuncContext *func_ctx, uint32 local_idx,
  435. uint32 offset, uint32 bytes);
  436. void
  437. aot_checked_addr_list_del(AOTFuncContext *func_ctx, uint32 local_idx);
  438. bool
  439. aot_checked_addr_list_find(AOTFuncContext *func_ctx, uint32 local_idx,
  440. uint32 offset, uint32 bytes);
  441. void
  442. aot_checked_addr_list_destroy(AOTFuncContext *func_ctx);
  443. bool
  444. aot_build_zero_function_ret(const AOTCompContext *comp_ctx,
  445. AOTFuncContext *func_ctx, AOTFuncType *func_type);
  446. LLVMValueRef
  447. aot_call_llvm_intrinsic(const AOTCompContext *comp_ctx,
  448. const AOTFuncContext *func_ctx, const char *intrinsic,
  449. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  450. int param_count, ...);
  451. LLVMValueRef
  452. aot_call_llvm_intrinsic_v(const AOTCompContext *comp_ctx,
  453. const AOTFuncContext *func_ctx, const char *intrinsic,
  454. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  455. int param_count, va_list param_value_list);
  456. LLVMValueRef
  457. aot_get_func_from_table(const AOTCompContext *comp_ctx, LLVMValueRef base,
  458. LLVMTypeRef func_type, int32 index);
  459. LLVMValueRef
  460. aot_load_const_from_table(AOTCompContext *comp_ctx, LLVMValueRef base,
  461. const WASMValue *value, uint8 value_type);
  462. bool
  463. aot_check_simd_compatibility(const char *arch_c_str, const char *cpu_c_str);
  464. void
  465. aot_add_expand_memory_op_pass(LLVMPassManagerRef pass);
  466. void
  467. aot_add_simple_loop_unswitch_pass(LLVMPassManagerRef pass);
  468. void
  469. aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module);
  470. void
  471. aot_handle_llvm_errmsg(const char *string, LLVMErrorRef err);
  472. char *
  473. aot_compress_aot_func_names(AOTCompContext *comp_ctx, uint32 *p_size);
  474. bool
  475. aot_set_cond_br_weights(AOTCompContext *comp_ctx, LLVMValueRef cond_br,
  476. int32 weights_true, int32 weights_false);
  477. bool
  478. aot_target_precheck_can_use_musttail(const AOTCompContext *comp_ctx);
  479. unsigned int
  480. aot_estimate_stack_usage_for_function_call(const AOTCompContext *comp_ctx,
  481. const AOTFuncType *callee_func_type);
  482. #ifdef __cplusplus
  483. } /* end of extern "C" */
  484. #endif
  485. #endif /* end of _AOT_LLVM_H_ */