aot_llvm.h 11 KB

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