aot_llvm.h 13 KB

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