aot_llvm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 aux_stack_bound;
  144. LLVMValueRef aux_stack_bottom;
  145. LLVMValueRef native_symbol;
  146. LLVMValueRef last_alloca;
  147. LLVMValueRef func_ptrs;
  148. AOTMemInfo *mem_info;
  149. LLVMValueRef cur_exception;
  150. bool mem_space_unchanged;
  151. AOTCheckedAddrList checked_addr_list;
  152. LLVMBasicBlockRef got_exception_block;
  153. LLVMBasicBlockRef func_return_block;
  154. LLVMValueRef exception_id_phi;
  155. LLVMValueRef func_type_indexes;
  156. #if WASM_ENABLE_DEBUG_AOT != 0
  157. LLVMMetadataRef debug_func;
  158. #endif
  159. LLVMValueRef locals[1];
  160. } AOTFuncContext;
  161. typedef struct AOTLLVMTypes {
  162. LLVMTypeRef int1_type;
  163. LLVMTypeRef int8_type;
  164. LLVMTypeRef int16_type;
  165. LLVMTypeRef int32_type;
  166. LLVMTypeRef int64_type;
  167. LLVMTypeRef float32_type;
  168. LLVMTypeRef float64_type;
  169. LLVMTypeRef void_type;
  170. LLVMTypeRef int8_ptr_type;
  171. LLVMTypeRef int8_pptr_type;
  172. LLVMTypeRef int16_ptr_type;
  173. LLVMTypeRef int32_ptr_type;
  174. LLVMTypeRef int64_ptr_type;
  175. LLVMTypeRef float32_ptr_type;
  176. LLVMTypeRef float64_ptr_type;
  177. LLVMTypeRef v128_type;
  178. LLVMTypeRef v128_ptr_type;
  179. LLVMTypeRef i8x16_vec_type;
  180. LLVMTypeRef i16x8_vec_type;
  181. LLVMTypeRef i32x4_vec_type;
  182. LLVMTypeRef i64x2_vec_type;
  183. LLVMTypeRef f32x4_vec_type;
  184. LLVMTypeRef f64x2_vec_type;
  185. LLVMTypeRef i1x2_vec_type;
  186. LLVMTypeRef meta_data_type;
  187. LLVMTypeRef funcref_type;
  188. LLVMTypeRef externref_type;
  189. } AOTLLVMTypes;
  190. typedef struct AOTLLVMConsts {
  191. LLVMValueRef i1_zero;
  192. LLVMValueRef i1_one;
  193. LLVMValueRef i8_zero;
  194. LLVMValueRef i32_zero;
  195. LLVMValueRef i64_zero;
  196. LLVMValueRef f32_zero;
  197. LLVMValueRef f64_zero;
  198. LLVMValueRef i32_one;
  199. LLVMValueRef i32_two;
  200. LLVMValueRef i32_three;
  201. LLVMValueRef i32_four;
  202. LLVMValueRef i32_five;
  203. LLVMValueRef i32_six;
  204. LLVMValueRef i32_seven;
  205. LLVMValueRef i32_eight;
  206. LLVMValueRef i32_nine;
  207. LLVMValueRef i32_ten;
  208. LLVMValueRef i32_eleven;
  209. LLVMValueRef i32_twelve;
  210. LLVMValueRef i32_thirteen;
  211. LLVMValueRef i32_fourteen;
  212. LLVMValueRef i32_fifteen;
  213. LLVMValueRef i32_neg_one;
  214. LLVMValueRef i64_neg_one;
  215. LLVMValueRef i32_min;
  216. LLVMValueRef i64_min;
  217. LLVMValueRef i32_31;
  218. LLVMValueRef i32_32;
  219. LLVMValueRef i64_63;
  220. LLVMValueRef i64_64;
  221. LLVMValueRef i8x16_vec_zero;
  222. LLVMValueRef i16x8_vec_zero;
  223. LLVMValueRef i32x4_vec_zero;
  224. LLVMValueRef i64x2_vec_zero;
  225. LLVMValueRef f32x4_vec_zero;
  226. LLVMValueRef f64x2_vec_zero;
  227. LLVMValueRef i8x16_undef;
  228. LLVMValueRef i16x8_undef;
  229. LLVMValueRef i32x4_undef;
  230. LLVMValueRef i64x2_undef;
  231. LLVMValueRef f32x4_undef;
  232. LLVMValueRef f64x2_undef;
  233. LLVMValueRef i32x16_zero;
  234. LLVMValueRef i32x8_zero;
  235. LLVMValueRef i32x4_zero;
  236. LLVMValueRef i32x2_zero;
  237. } AOTLLVMConsts;
  238. /**
  239. * Compiler context
  240. */
  241. typedef struct AOTCompContext {
  242. AOTCompData *comp_data;
  243. /* LLVM variables required to emit LLVM IR */
  244. LLVMContextRef context;
  245. LLVMBuilderRef builder;
  246. #if WASM_ENABLE_DEBUG_AOT
  247. LLVMDIBuilderRef debug_builder;
  248. LLVMMetadataRef debug_file;
  249. LLVMMetadataRef debug_comp_unit;
  250. #endif
  251. LLVMTargetMachineRef target_machine;
  252. char *target_cpu;
  253. char target_arch[16];
  254. unsigned pointer_size;
  255. /* Hardware intrinsic compability flags */
  256. uint64 flags[8];
  257. /* required by JIT */
  258. LLVMOrcLLLazyJITRef orc_jit;
  259. LLVMOrcThreadSafeContextRef orc_thread_safe_context;
  260. LLVMModuleRef module;
  261. bool is_jit_mode;
  262. /* AOT indirect mode flag & symbol list */
  263. bool is_indirect_mode;
  264. bh_list native_symbols;
  265. /* Bulk memory feature */
  266. bool enable_bulk_memory;
  267. /* Bounday Check */
  268. bool enable_bound_check;
  269. /* 128-bit SIMD */
  270. bool enable_simd;
  271. /* Auxiliary stack overflow/underflow check */
  272. bool enable_aux_stack_check;
  273. /* Generate auxiliary stack frame */
  274. bool enable_aux_stack_frame;
  275. /* Thread Manager */
  276. bool enable_thread_mgr;
  277. /* Tail Call */
  278. bool enable_tail_call;
  279. /* Reference Types */
  280. bool enable_ref_types;
  281. /* Disable LLVM built-in intrinsics */
  282. bool disable_llvm_intrinsics;
  283. /* Disable LLVM link time optimization */
  284. bool disable_llvm_lto;
  285. /* Whether optimize the JITed code */
  286. bool optimize;
  287. uint32 opt_level;
  288. uint32 size_level;
  289. /* LLVM floating-point rounding mode metadata */
  290. LLVMValueRef fp_rounding_mode;
  291. /* LLVM floating-point exception behavior metadata */
  292. LLVMValueRef fp_exception_behavior;
  293. /* LLVM data types */
  294. AOTLLVMTypes basic_types;
  295. LLVMTypeRef exec_env_type;
  296. LLVMTypeRef aot_inst_type;
  297. /* LLVM const values */
  298. AOTLLVMConsts llvm_consts;
  299. /* Function contexts */
  300. /* TODO: */
  301. AOTFuncContext **func_ctxes;
  302. uint32 func_ctx_count;
  303. char **custom_sections_wp;
  304. uint32 custom_sections_count;
  305. /* 3rd-party toolchains */
  306. /* External llc compiler, if specified, wamrc will emit the llvm-ir file and
  307. * invoke the llc compiler to generate object file.
  308. * This can be used when we want to benefit from the optimization of other
  309. * LLVM based toolchains */
  310. const char *external_llc_compiler;
  311. const char *llc_compiler_flags;
  312. /* External asm compiler, if specified, wamrc will emit the text-based
  313. * assembly file (.s) and invoke the llc compiler to generate object file.
  314. * This will be useful when the upstream LLVM doesn't support to emit object
  315. * file for some architecture (such as arc) */
  316. const char *external_asm_compiler;
  317. const char *asm_compiler_flags;
  318. } AOTCompContext;
  319. enum {
  320. AOT_FORMAT_FILE,
  321. AOT_OBJECT_FILE,
  322. AOT_LLVMIR_UNOPT_FILE,
  323. AOT_LLVMIR_OPT_FILE,
  324. };
  325. typedef struct AOTCompOption {
  326. bool is_jit_mode;
  327. bool is_indirect_mode;
  328. char *target_arch;
  329. char *target_abi;
  330. char *target_cpu;
  331. char *cpu_features;
  332. bool is_sgx_platform;
  333. bool enable_bulk_memory;
  334. bool enable_thread_mgr;
  335. bool enable_tail_call;
  336. bool enable_simd;
  337. bool enable_ref_types;
  338. bool enable_aux_stack_check;
  339. bool enable_aux_stack_frame;
  340. bool disable_llvm_intrinsics;
  341. bool disable_llvm_lto;
  342. uint32 opt_level;
  343. uint32 size_level;
  344. uint32 output_format;
  345. uint32 bounds_checks;
  346. char **custom_sections;
  347. uint32 custom_sections_count;
  348. } AOTCompOption, *aot_comp_option_t;
  349. AOTCompContext *
  350. aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option);
  351. void
  352. aot_destroy_comp_context(AOTCompContext *comp_ctx);
  353. int32
  354. aot_get_native_symbol_index(AOTCompContext *comp_ctx, const char *symbol);
  355. bool
  356. aot_compile_wasm(AOTCompContext *comp_ctx);
  357. uint8 *
  358. aot_emit_elf_file(AOTCompContext *comp_ctx, uint32 *p_elf_file_size);
  359. void
  360. aot_destroy_elf_file(uint8 *elf_file);
  361. void
  362. aot_value_stack_push(AOTValueStack *stack, AOTValue *value);
  363. AOTValue *
  364. aot_value_stack_pop(AOTValueStack *stack);
  365. void
  366. aot_value_stack_destroy(AOTValueStack *stack);
  367. void
  368. aot_block_stack_push(AOTBlockStack *stack, AOTBlock *block);
  369. AOTBlock *
  370. aot_block_stack_pop(AOTBlockStack *stack);
  371. void
  372. aot_block_stack_destroy(AOTBlockStack *stack);
  373. void
  374. aot_block_destroy(AOTBlock *block);
  375. LLVMTypeRef
  376. wasm_type_to_llvm_type(AOTLLVMTypes *llvm_types, uint8 wasm_type);
  377. bool
  378. aot_checked_addr_list_add(AOTFuncContext *func_ctx, uint32 local_idx,
  379. uint32 offset, uint32 bytes);
  380. void
  381. aot_checked_addr_list_del(AOTFuncContext *func_ctx, uint32 local_idx);
  382. bool
  383. aot_checked_addr_list_find(AOTFuncContext *func_ctx, uint32 local_idx,
  384. uint32 offset, uint32 bytes);
  385. void
  386. aot_checked_addr_list_destroy(AOTFuncContext *func_ctx);
  387. bool
  388. aot_build_zero_function_ret(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  389. AOTFuncType *func_type);
  390. LLVMValueRef
  391. aot_call_llvm_intrinsic(const AOTCompContext *comp_ctx,
  392. const AOTFuncContext *func_ctx, const char *intrinsic,
  393. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  394. int param_count, ...);
  395. LLVMValueRef
  396. aot_call_llvm_intrinsic_v(const AOTCompContext *comp_ctx,
  397. const AOTFuncContext *func_ctx, const char *intrinsic,
  398. LLVMTypeRef ret_type, LLVMTypeRef *param_types,
  399. int param_count, va_list param_value_list);
  400. LLVMValueRef
  401. aot_get_func_from_table(const AOTCompContext *comp_ctx, LLVMValueRef base,
  402. LLVMTypeRef func_type, int32 index);
  403. LLVMValueRef
  404. aot_load_const_from_table(AOTCompContext *comp_ctx, LLVMValueRef base,
  405. const WASMValue *value, uint8 value_type);
  406. bool
  407. aot_check_simd_compatibility(const char *arch_c_str, const char *cpu_c_str);
  408. void
  409. aot_add_expand_memory_op_pass(LLVMPassManagerRef pass);
  410. void
  411. aot_add_simple_loop_unswitch_pass(LLVMPassManagerRef pass);
  412. void
  413. aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module);
  414. void
  415. aot_handle_llvm_errmsg(const char *string, LLVMErrorRef err);
  416. #ifdef __cplusplus
  417. } /* end of extern "C" */
  418. #endif
  419. #endif /* end of _AOT_LLVM_H_ */