aot_llvm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. /* Bounday Check */
  278. bool enable_bound_check;
  279. /* Native stack bounday 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. /* Use profile file collected by LLVM PGO */
  302. char *use_prof_file;
  303. /* Enable to use segument register as the base addr
  304. of linear memory for load/store operations */
  305. bool enable_segue_i32_load;
  306. bool enable_segue_i64_load;
  307. bool enable_segue_f32_load;
  308. bool enable_segue_f64_load;
  309. bool enable_segue_v128_load;
  310. bool enable_segue_i32_store;
  311. bool enable_segue_i64_store;
  312. bool enable_segue_f32_store;
  313. bool enable_segue_f64_store;
  314. bool enable_segue_v128_store;
  315. /* Whether optimize the JITed code */
  316. bool optimize;
  317. uint32 opt_level;
  318. uint32 size_level;
  319. /* LLVM floating-point rounding mode metadata */
  320. LLVMValueRef fp_rounding_mode;
  321. /* LLVM floating-point exception behavior metadata */
  322. LLVMValueRef fp_exception_behavior;
  323. /* a global array to store stack sizes */
  324. LLVMTypeRef stack_sizes_type;
  325. LLVMValueRef stack_sizes;
  326. uint32 *jit_stack_sizes; /* for JIT */
  327. /* LLVM data types */
  328. AOTLLVMTypes basic_types;
  329. LLVMTypeRef exec_env_type;
  330. LLVMTypeRef aot_inst_type;
  331. /* LLVM const values */
  332. AOTLLVMConsts llvm_consts;
  333. /* Function contexts */
  334. /* TODO: */
  335. AOTFuncContext **func_ctxes;
  336. uint32 func_ctx_count;
  337. char **custom_sections_wp;
  338. uint32 custom_sections_count;
  339. /* 3rd-party toolchains */
  340. /* External llc compiler, if specified, wamrc will emit the llvm-ir file and
  341. * invoke the llc compiler to generate object file.
  342. * This can be used when we want to benefit from the optimization of other
  343. * LLVM based toolchains */
  344. const char *external_llc_compiler;
  345. const char *llc_compiler_flags;
  346. /* External asm compiler, if specified, wamrc will emit the text-based
  347. * assembly file (.s) and invoke the llc compiler to generate object file.
  348. * This will be useful when the upstream LLVM doesn't support to emit object
  349. * file for some architecture (such as arc) */
  350. const char *external_asm_compiler;
  351. const char *asm_compiler_flags;
  352. const char *stack_usage_file;
  353. char stack_usage_temp_file[64];
  354. const char *llvm_passes;
  355. const char *builtin_intrinsics;
  356. bool emit_frame_pointer;
  357. } AOTCompContext;
  358. enum {
  359. AOT_FORMAT_FILE,
  360. AOT_OBJECT_FILE,
  361. AOT_LLVMIR_UNOPT_FILE,
  362. AOT_LLVMIR_OPT_FILE,
  363. };
  364. /* always sync it with AOTCompOption in aot_export.h */
  365. typedef struct AOTCompOption {
  366. bool is_jit_mode;
  367. bool is_indirect_mode;
  368. char *target_arch;
  369. char *target_abi;
  370. char *target_cpu;
  371. char *cpu_features;
  372. bool is_sgx_platform;
  373. bool enable_bulk_memory;
  374. bool enable_thread_mgr;
  375. bool enable_tail_call;
  376. bool enable_simd;
  377. bool enable_ref_types;
  378. bool enable_aux_stack_check;
  379. bool enable_aux_stack_frame;
  380. bool disable_llvm_intrinsics;
  381. bool disable_llvm_lto;
  382. bool enable_llvm_pgo;
  383. bool enable_stack_estimation;
  384. char *use_prof_file;
  385. uint32 opt_level;
  386. uint32 size_level;
  387. uint32 output_format;
  388. uint32 bounds_checks;
  389. uint32 stack_bounds_checks;
  390. uint32 segue_flags;
  391. bool linux_perf_support;
  392. char **custom_sections;
  393. uint32 custom_sections_count;
  394. const char *stack_usage_file;
  395. const char *llvm_passes;
  396. const char *builtin_intrinsics;
  397. } AOTCompOption, *aot_comp_option_t;
  398. bool
  399. aot_compiler_init(void);
  400. void
  401. aot_compiler_destroy(void);
  402. AOTCompContext *
  403. aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option);
  404. void
  405. aot_destroy_comp_context(AOTCompContext *comp_ctx);
  406. int32
  407. aot_get_native_symbol_index(AOTCompContext *comp_ctx, const char *symbol);
  408. bool
  409. aot_compile_wasm(AOTCompContext *comp_ctx);
  410. uint8 *
  411. aot_emit_elf_file(AOTCompContext *comp_ctx, uint32 *p_elf_file_size);
  412. void
  413. aot_destroy_elf_file(uint8 *elf_file);
  414. void
  415. aot_value_stack_push(AOTValueStack *stack, AOTValue *value);
  416. AOTValue *
  417. aot_value_stack_pop(AOTValueStack *stack);
  418. void
  419. aot_value_stack_destroy(AOTValueStack *stack);
  420. void
  421. aot_block_stack_push(AOTBlockStack *stack, AOTBlock *block);
  422. AOTBlock *
  423. aot_block_stack_pop(AOTBlockStack *stack);
  424. void
  425. aot_block_stack_destroy(AOTBlockStack *stack);
  426. void
  427. aot_block_destroy(AOTBlock *block);
  428. LLVMTypeRef
  429. wasm_type_to_llvm_type(const AOTLLVMTypes *llvm_types, uint8 wasm_type);
  430. bool
  431. aot_checked_addr_list_add(AOTFuncContext *func_ctx, uint32 local_idx,
  432. uint32 offset, uint32 bytes);
  433. void
  434. aot_checked_addr_list_del(AOTFuncContext *func_ctx, uint32 local_idx);
  435. bool
  436. aot_checked_addr_list_find(AOTFuncContext *func_ctx, uint32 local_idx,
  437. uint32 offset, uint32 bytes);
  438. void
  439. aot_checked_addr_list_destroy(AOTFuncContext *func_ctx);
  440. bool
  441. aot_build_zero_function_ret(const AOTCompContext *comp_ctx,
  442. AOTFuncContext *func_ctx, AOTFuncType *func_type);
  443. LLVMValueRef
  444. aot_call_llvm_intrinsic(const AOTCompContext *comp_ctx,
  445. const AOTFuncContext *func_ctx, const char *intrinsic,
  446. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  447. int param_count, ...);
  448. LLVMValueRef
  449. aot_call_llvm_intrinsic_v(const AOTCompContext *comp_ctx,
  450. const AOTFuncContext *func_ctx, const char *intrinsic,
  451. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  452. int param_count, va_list param_value_list);
  453. LLVMValueRef
  454. aot_get_func_from_table(const AOTCompContext *comp_ctx, LLVMValueRef base,
  455. LLVMTypeRef func_type, int32 index);
  456. LLVMValueRef
  457. aot_load_const_from_table(AOTCompContext *comp_ctx, LLVMValueRef base,
  458. const WASMValue *value, uint8 value_type);
  459. bool
  460. aot_check_simd_compatibility(const char *arch_c_str, const char *cpu_c_str);
  461. void
  462. aot_add_expand_memory_op_pass(LLVMPassManagerRef pass);
  463. void
  464. aot_add_simple_loop_unswitch_pass(LLVMPassManagerRef pass);
  465. void
  466. aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module);
  467. void
  468. aot_handle_llvm_errmsg(const char *string, LLVMErrorRef err);
  469. char *
  470. aot_compress_aot_func_names(AOTCompContext *comp_ctx, uint32 *p_size);
  471. bool
  472. aot_set_cond_br_weights(AOTCompContext *comp_ctx, LLVMValueRef cond_br,
  473. int32 weights_true, int32 weights_false);
  474. bool
  475. aot_target_precheck_can_use_musttail(const AOTCompContext *comp_ctx);
  476. unsigned int
  477. aot_estimate_stack_usage_for_function_call(const AOTCompContext *comp_ctx,
  478. const AOTFuncType *callee_func_type);
  479. #ifdef __cplusplus
  480. } /* end of extern "C" */
  481. #endif
  482. #endif /* end of _AOT_LLVM_H_ */