aot_llvm.h 11 KB

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