aot_emit_control.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_control.h"
  6. #include "aot_emit_exception.h"
  7. #include "../aot/aot_runtime.h"
  8. #include "../interpreter/wasm_loader.h"
  9. #if WASM_ENABLE_DEBUG_AOT != 0
  10. #include "debug/dwarf_extractor.h"
  11. #endif
  12. static char *block_name_prefix[] = { "block", "loop", "if" };
  13. static char *block_name_suffix[] = { "begin", "else", "end" };
  14. /* clang-format off */
  15. enum {
  16. LABEL_BEGIN = 0,
  17. LABEL_ELSE,
  18. LABEL_END
  19. };
  20. /* clang-format on */
  21. static void
  22. format_block_name(char *name, uint32 name_size, uint32 block_index,
  23. uint32 label_type, uint32 label_id)
  24. {
  25. if (label_type != LABEL_TYPE_FUNCTION)
  26. snprintf(name, name_size, "%s%d%s%s", block_name_prefix[label_type],
  27. block_index, "_", block_name_suffix[label_id]);
  28. else
  29. snprintf(name, name_size, "%s", "func_end");
  30. }
  31. #define CREATE_BLOCK(new_llvm_block, name) \
  32. do { \
  33. if (!(new_llvm_block = LLVMAppendBasicBlockInContext( \
  34. comp_ctx->context, func_ctx->func, name))) { \
  35. aot_set_last_error("add LLVM basic block failed."); \
  36. goto fail; \
  37. } \
  38. } while (0)
  39. #define CURR_BLOCK() LLVMGetInsertBlock(comp_ctx->builder)
  40. #define MOVE_BLOCK_AFTER(llvm_block, llvm_block_after) \
  41. LLVMMoveBasicBlockAfter(llvm_block, llvm_block_after)
  42. #define MOVE_BLOCK_AFTER_CURR(llvm_block) \
  43. LLVMMoveBasicBlockAfter(llvm_block, CURR_BLOCK())
  44. #define MOVE_BLOCK_BEFORE(llvm_block, llvm_block_before) \
  45. LLVMMoveBasicBlockBefore(llvm_block, llvm_block_before)
  46. #define BUILD_BR(llvm_block) \
  47. do { \
  48. if (!LLVMBuildBr(comp_ctx->builder, llvm_block)) { \
  49. aot_set_last_error("llvm build br failed."); \
  50. goto fail; \
  51. } \
  52. } while (0)
  53. #define BUILD_COND_BR(value_if, block_then, block_else) \
  54. do { \
  55. if (!LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
  56. block_else)) { \
  57. aot_set_last_error("llvm build cond br failed."); \
  58. goto fail; \
  59. } \
  60. } while (0)
  61. #define SET_BUILDER_POS(llvm_block) \
  62. LLVMPositionBuilderAtEnd(comp_ctx->builder, llvm_block)
  63. #define CREATE_RESULT_VALUE_PHIS(block) \
  64. do { \
  65. if (block->result_count && !block->result_phis) { \
  66. uint32 _i; \
  67. uint64 _size; \
  68. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  69. /* Allocate memory */ \
  70. _size = sizeof(LLVMValueRef) * (uint64)block->result_count; \
  71. if (_size >= UINT32_MAX \
  72. || !(block->result_phis = \
  73. wasm_runtime_malloc((uint32)_size))) { \
  74. aot_set_last_error("allocate memory failed."); \
  75. goto fail; \
  76. } \
  77. SET_BUILDER_POS(block->llvm_end_block); \
  78. for (_i = 0; _i < block->result_count; _i++) { \
  79. if (!(block->result_phis[_i] = LLVMBuildPhi( \
  80. comp_ctx->builder, \
  81. TO_LLVM_TYPE(block->result_types[_i]), "phi"))) { \
  82. aot_set_last_error("llvm build phi failed."); \
  83. goto fail; \
  84. } \
  85. } \
  86. SET_BUILDER_POS(_block_curr); \
  87. } \
  88. } while (0)
  89. #define ADD_TO_RESULT_PHIS(block, value, idx) \
  90. do { \
  91. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  92. LLVMTypeRef phi_ty = LLVMTypeOf(block->result_phis[idx]); \
  93. LLVMTypeRef value_ty = LLVMTypeOf(value); \
  94. bh_assert(LLVMGetTypeKind(phi_ty) == LLVMGetTypeKind(value_ty)); \
  95. bh_assert(LLVMGetTypeContext(phi_ty) == LLVMGetTypeContext(value_ty)); \
  96. LLVMAddIncoming(block->result_phis[idx], &value, &_block_curr, 1); \
  97. (void)phi_ty; \
  98. (void)value_ty; \
  99. } while (0)
  100. #define BUILD_ICMP(op, left, right, res, name) \
  101. do { \
  102. if (!(res = \
  103. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  104. aot_set_last_error("llvm build icmp failed."); \
  105. goto fail; \
  106. } \
  107. } while (0)
  108. #define ADD_TO_PARAM_PHIS(block, value, idx) \
  109. do { \
  110. LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \
  111. LLVMAddIncoming(block->param_phis[idx], &value, &_block_curr, 1); \
  112. } while (0)
  113. static LLVMBasicBlockRef
  114. find_next_llvm_end_block(AOTBlock *block)
  115. {
  116. block = block->prev;
  117. while (block && !block->llvm_end_block)
  118. block = block->prev;
  119. return block ? block->llvm_end_block : NULL;
  120. }
  121. static AOTBlock *
  122. get_target_block(AOTFuncContext *func_ctx, uint32 br_depth)
  123. {
  124. uint32 i = br_depth;
  125. AOTBlock *block = func_ctx->block_stack.block_list_end;
  126. while (i-- > 0 && block) {
  127. block = block->prev;
  128. }
  129. if (!block) {
  130. aot_set_last_error("WASM block stack underflow.");
  131. return NULL;
  132. }
  133. return block;
  134. }
  135. static bool
  136. handle_next_reachable_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  137. uint8 **p_frame_ip)
  138. {
  139. AOTBlock *block = func_ctx->block_stack.block_list_end;
  140. AOTBlock *block_prev;
  141. uint8 *frame_ip = NULL;
  142. uint32 i;
  143. AOTFuncType *func_type;
  144. LLVMValueRef ret;
  145. #if WASM_ENABLE_DEBUG_AOT != 0
  146. LLVMMetadataRef return_location;
  147. #endif
  148. aot_checked_addr_list_destroy(func_ctx);
  149. bh_assert(block);
  150. #if WASM_ENABLE_DEBUG_AOT != 0
  151. return_location = dwarf_gen_location(
  152. comp_ctx, func_ctx,
  153. (*p_frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  154. #endif
  155. if (block->label_type == LABEL_TYPE_IF && block->llvm_else_block
  156. && *p_frame_ip <= block->wasm_code_else) {
  157. /* Clear value stack and start to translate else branch */
  158. aot_value_stack_destroy(&block->value_stack);
  159. /* Recover parameters of else branch */
  160. for (i = 0; i < block->param_count; i++)
  161. PUSH(block->else_param_phis[i], block->param_types[i]);
  162. SET_BUILDER_POS(block->llvm_else_block);
  163. *p_frame_ip = block->wasm_code_else + 1;
  164. return true;
  165. }
  166. while (block && !block->is_reachable) {
  167. block_prev = block->prev;
  168. block = aot_block_stack_pop(&func_ctx->block_stack);
  169. if (block->label_type == LABEL_TYPE_IF) {
  170. if (block->llvm_else_block && !block->skip_wasm_code_else
  171. && *p_frame_ip <= block->wasm_code_else) {
  172. /* Clear value stack and start to translate else branch */
  173. aot_value_stack_destroy(&block->value_stack);
  174. SET_BUILDER_POS(block->llvm_else_block);
  175. *p_frame_ip = block->wasm_code_else + 1;
  176. /* Push back the block */
  177. aot_block_stack_push(&func_ctx->block_stack, block);
  178. return true;
  179. }
  180. else if (block->llvm_end_block) {
  181. /* Remove unreachable basic block */
  182. LLVMDeleteBasicBlock(block->llvm_end_block);
  183. block->llvm_end_block = NULL;
  184. }
  185. }
  186. frame_ip = block->wasm_code_end;
  187. aot_block_destroy(block);
  188. block = block_prev;
  189. }
  190. if (!block) {
  191. *p_frame_ip = frame_ip + 1;
  192. return true;
  193. }
  194. *p_frame_ip = block->wasm_code_end + 1;
  195. SET_BUILDER_POS(block->llvm_end_block);
  196. /* Pop block, push its return value, and destroy the block */
  197. block = aot_block_stack_pop(&func_ctx->block_stack);
  198. func_type = func_ctx->aot_func->func_type;
  199. for (i = 0; i < block->result_count; i++) {
  200. bh_assert(block->result_phis[i]);
  201. if (block->label_type != LABEL_TYPE_FUNCTION) {
  202. PUSH(block->result_phis[i], block->result_types[i]);
  203. }
  204. else {
  205. /* Store extra return values to function parameters */
  206. if (i != 0) {
  207. uint32 param_index = func_type->param_count + i;
  208. if (!LLVMBuildStore(
  209. comp_ctx->builder, block->result_phis[i],
  210. LLVMGetParam(func_ctx->func, param_index))) {
  211. aot_set_last_error("llvm build store failed.");
  212. goto fail;
  213. }
  214. }
  215. }
  216. }
  217. if (block->label_type == LABEL_TYPE_FUNCTION) {
  218. if (block->result_count) {
  219. /* Return the first return value */
  220. if (!(ret =
  221. LLVMBuildRet(comp_ctx->builder, block->result_phis[0]))) {
  222. aot_set_last_error("llvm build return failed.");
  223. goto fail;
  224. }
  225. #if WASM_ENABLE_DEBUG_AOT != 0
  226. LLVMInstructionSetDebugLoc(ret, return_location);
  227. #endif
  228. }
  229. else {
  230. if (!(ret = LLVMBuildRetVoid(comp_ctx->builder))) {
  231. aot_set_last_error("llvm build return void failed.");
  232. goto fail;
  233. }
  234. #if WASM_ENABLE_DEBUG_AOT != 0
  235. LLVMInstructionSetDebugLoc(ret, return_location);
  236. #endif
  237. }
  238. }
  239. aot_block_destroy(block);
  240. return true;
  241. fail:
  242. return false;
  243. }
  244. static bool
  245. push_aot_block_to_stack_and_pass_params(AOTCompContext *comp_ctx,
  246. AOTFuncContext *func_ctx,
  247. AOTBlock *block)
  248. {
  249. uint32 i, param_index;
  250. LLVMValueRef value;
  251. uint64 size;
  252. char name[32];
  253. LLVMBasicBlockRef block_curr = CURR_BLOCK();
  254. if (block->param_count) {
  255. size = sizeof(LLVMValueRef) * (uint64)block->param_count;
  256. if (size >= UINT32_MAX
  257. || !(block->param_phis = wasm_runtime_malloc((uint32)size))) {
  258. aot_set_last_error("allocate memory failed.");
  259. return false;
  260. }
  261. if (block->label_type == LABEL_TYPE_IF && !block->skip_wasm_code_else
  262. && !(block->else_param_phis = wasm_runtime_malloc((uint32)size))) {
  263. wasm_runtime_free(block->param_phis);
  264. block->param_phis = NULL;
  265. aot_set_last_error("allocate memory failed.");
  266. return false;
  267. }
  268. /* Create param phis */
  269. for (i = 0; i < block->param_count; i++) {
  270. SET_BUILDER_POS(block->llvm_entry_block);
  271. snprintf(name, sizeof(name), "%s%d_phi%d",
  272. block_name_prefix[block->label_type], block->block_index,
  273. i);
  274. if (!(block->param_phis[i] = LLVMBuildPhi(
  275. comp_ctx->builder, TO_LLVM_TYPE(block->param_types[i]),
  276. name))) {
  277. aot_set_last_error("llvm build phi failed.");
  278. goto fail;
  279. }
  280. if (block->label_type == LABEL_TYPE_IF
  281. && !block->skip_wasm_code_else && block->llvm_else_block) {
  282. /* Build else param phis */
  283. SET_BUILDER_POS(block->llvm_else_block);
  284. snprintf(name, sizeof(name), "else%d_phi%d", block->block_index,
  285. i);
  286. if (!(block->else_param_phis[i] = LLVMBuildPhi(
  287. comp_ctx->builder,
  288. TO_LLVM_TYPE(block->param_types[i]), name))) {
  289. aot_set_last_error("llvm build phi failed.");
  290. goto fail;
  291. }
  292. }
  293. }
  294. SET_BUILDER_POS(block_curr);
  295. /* Pop param values from current block's
  296. * value stack and add to param phis.
  297. */
  298. for (i = 0; i < block->param_count; i++) {
  299. param_index = block->param_count - 1 - i;
  300. POP(value, block->param_types[param_index]);
  301. ADD_TO_PARAM_PHIS(block, value, param_index);
  302. if (block->label_type == LABEL_TYPE_IF
  303. && !block->skip_wasm_code_else) {
  304. if (block->llvm_else_block) {
  305. /* has else branch, add to else param phis */
  306. LLVMAddIncoming(block->else_param_phis[param_index], &value,
  307. &block_curr, 1);
  308. }
  309. else {
  310. /* no else branch, add to result phis */
  311. CREATE_RESULT_VALUE_PHIS(block);
  312. ADD_TO_RESULT_PHIS(block, value, param_index);
  313. }
  314. }
  315. }
  316. }
  317. /* Push the new block to block stack */
  318. aot_block_stack_push(&func_ctx->block_stack, block);
  319. /* Push param phis to the new block */
  320. for (i = 0; i < block->param_count; i++) {
  321. PUSH(block->param_phis[i], block->param_types[i]);
  322. }
  323. return true;
  324. fail:
  325. if (block->param_phis) {
  326. wasm_runtime_free(block->param_phis);
  327. block->param_phis = NULL;
  328. }
  329. if (block->else_param_phis) {
  330. wasm_runtime_free(block->else_param_phis);
  331. block->else_param_phis = NULL;
  332. }
  333. return false;
  334. }
  335. bool
  336. aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  337. uint8 **p_frame_ip, uint8 *frame_ip_end, uint32 label_type,
  338. uint32 param_count, uint8 *param_types,
  339. uint32 result_count, uint8 *result_types)
  340. {
  341. BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
  342. AOTBlock *block;
  343. uint8 *else_addr, *end_addr;
  344. LLVMValueRef value;
  345. char name[32];
  346. /* Check block stack */
  347. if (!func_ctx->block_stack.block_list_end) {
  348. aot_set_last_error("WASM block stack underflow.");
  349. return false;
  350. }
  351. memset(block_addr_cache, 0, sizeof(block_addr_cache));
  352. /* Get block info */
  353. if (!(wasm_loader_find_block_addr(
  354. NULL, (BlockAddr *)block_addr_cache, *p_frame_ip, frame_ip_end,
  355. (uint8)label_type, &else_addr, &end_addr))) {
  356. aot_set_last_error("find block end addr failed.");
  357. return false;
  358. }
  359. /* Allocate memory */
  360. if (!(block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
  361. aot_set_last_error("allocate memory failed.");
  362. return false;
  363. }
  364. memset(block, 0, sizeof(AOTBlock));
  365. if (param_count
  366. && !(block->param_types = wasm_runtime_malloc(param_count))) {
  367. aot_set_last_error("allocate memory failed.");
  368. goto fail;
  369. }
  370. if (result_count) {
  371. if (!(block->result_types = wasm_runtime_malloc(result_count))) {
  372. aot_set_last_error("allocate memory failed.");
  373. goto fail;
  374. }
  375. }
  376. /* Init aot block data */
  377. block->label_type = label_type;
  378. block->param_count = param_count;
  379. if (param_count) {
  380. bh_memcpy_s(block->param_types, param_count, param_types, param_count);
  381. }
  382. block->result_count = result_count;
  383. if (result_count) {
  384. bh_memcpy_s(block->result_types, result_count, result_types,
  385. result_count);
  386. }
  387. block->wasm_code_else = else_addr;
  388. block->wasm_code_end = end_addr;
  389. block->block_index = func_ctx->block_stack.block_index[label_type];
  390. func_ctx->block_stack.block_index[label_type]++;
  391. if (label_type == LABEL_TYPE_BLOCK || label_type == LABEL_TYPE_LOOP) {
  392. /* Create block */
  393. format_block_name(name, sizeof(name), block->block_index, label_type,
  394. LABEL_BEGIN);
  395. CREATE_BLOCK(block->llvm_entry_block, name);
  396. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  397. /* Jump to the entry block */
  398. BUILD_BR(block->llvm_entry_block);
  399. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx, block))
  400. goto fail;
  401. /* Start to translate the block */
  402. SET_BUILDER_POS(block->llvm_entry_block);
  403. if (label_type == LABEL_TYPE_LOOP)
  404. aot_checked_addr_list_destroy(func_ctx);
  405. }
  406. else if (label_type == LABEL_TYPE_IF) {
  407. POP_COND(value);
  408. if (LLVMIsUndef(value)
  409. #if LLVM_VERSION_NUMBER >= 12
  410. || LLVMIsPoison(value)
  411. #endif
  412. ) {
  413. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  414. false, NULL, NULL))) {
  415. goto fail;
  416. }
  417. aot_block_destroy(block);
  418. return aot_handle_next_reachable_block(comp_ctx, func_ctx,
  419. p_frame_ip);
  420. }
  421. if (!LLVMIsConstant(value)) {
  422. /* Compare value is not constant, create condition br IR */
  423. /* Create entry block */
  424. format_block_name(name, sizeof(name), block->block_index,
  425. label_type, LABEL_BEGIN);
  426. CREATE_BLOCK(block->llvm_entry_block, name);
  427. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  428. /* Create end block */
  429. format_block_name(name, sizeof(name), block->block_index,
  430. label_type, LABEL_END);
  431. CREATE_BLOCK(block->llvm_end_block, name);
  432. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_entry_block);
  433. if (else_addr) {
  434. /* Create else block */
  435. format_block_name(name, sizeof(name), block->block_index,
  436. label_type, LABEL_ELSE);
  437. CREATE_BLOCK(block->llvm_else_block, name);
  438. MOVE_BLOCK_AFTER(block->llvm_else_block,
  439. block->llvm_entry_block);
  440. /* Create condition br IR */
  441. BUILD_COND_BR(value, block->llvm_entry_block,
  442. block->llvm_else_block);
  443. }
  444. else {
  445. /* Create condition br IR */
  446. BUILD_COND_BR(value, block->llvm_entry_block,
  447. block->llvm_end_block);
  448. block->is_reachable = true;
  449. }
  450. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  451. block))
  452. goto fail;
  453. /* Start to translate if branch of BLOCK if */
  454. SET_BUILDER_POS(block->llvm_entry_block);
  455. }
  456. else {
  457. if ((int32)LLVMConstIntGetZExtValue(value) != 0) {
  458. /* Compare value is not 0, condition is true, else branch of
  459. BLOCK if cannot be reached */
  460. block->skip_wasm_code_else = true;
  461. /* Create entry block */
  462. format_block_name(name, sizeof(name), block->block_index,
  463. label_type, LABEL_BEGIN);
  464. CREATE_BLOCK(block->llvm_entry_block, name);
  465. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  466. /* Jump to the entry block */
  467. BUILD_BR(block->llvm_entry_block);
  468. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  469. block))
  470. goto fail;
  471. /* Start to translate the if branch */
  472. SET_BUILDER_POS(block->llvm_entry_block);
  473. }
  474. else {
  475. /* Compare value is not 0, condition is false, if branch of
  476. BLOCK if cannot be reached */
  477. if (else_addr) {
  478. /* Create else block */
  479. format_block_name(name, sizeof(name), block->block_index,
  480. label_type, LABEL_ELSE);
  481. CREATE_BLOCK(block->llvm_else_block, name);
  482. MOVE_BLOCK_AFTER_CURR(block->llvm_else_block);
  483. /* Jump to the else block */
  484. BUILD_BR(block->llvm_else_block);
  485. if (!push_aot_block_to_stack_and_pass_params(
  486. comp_ctx, func_ctx, block))
  487. goto fail;
  488. /* Start to translate the else branch */
  489. SET_BUILDER_POS(block->llvm_else_block);
  490. *p_frame_ip = else_addr + 1;
  491. }
  492. else {
  493. /* skip the block */
  494. aot_block_destroy(block);
  495. *p_frame_ip = end_addr + 1;
  496. }
  497. }
  498. }
  499. }
  500. else {
  501. aot_set_last_error("Invalid block type.");
  502. goto fail;
  503. }
  504. return true;
  505. fail:
  506. aot_block_destroy(block);
  507. return false;
  508. }
  509. bool
  510. aot_compile_op_else(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  511. uint8 **p_frame_ip)
  512. {
  513. AOTBlock *block = func_ctx->block_stack.block_list_end;
  514. LLVMValueRef value;
  515. char name[32];
  516. uint32 i, result_index;
  517. /* Check block */
  518. if (!block) {
  519. aot_set_last_error("WASM block stack underflow.");
  520. return false;
  521. }
  522. if (block->label_type != LABEL_TYPE_IF
  523. || (!block->skip_wasm_code_else && !block->llvm_else_block)) {
  524. aot_set_last_error("Invalid WASM block type.");
  525. return false;
  526. }
  527. /* Create end block if needed */
  528. if (!block->llvm_end_block) {
  529. format_block_name(name, sizeof(name), block->block_index,
  530. block->label_type, LABEL_END);
  531. CREATE_BLOCK(block->llvm_end_block, name);
  532. if (block->llvm_else_block)
  533. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_else_block);
  534. else
  535. MOVE_BLOCK_AFTER_CURR(block->llvm_end_block);
  536. }
  537. block->is_reachable = true;
  538. /* Comes from the if branch of BLOCK if */
  539. CREATE_RESULT_VALUE_PHIS(block);
  540. for (i = 0; i < block->result_count; i++) {
  541. result_index = block->result_count - 1 - i;
  542. POP(value, block->result_types[result_index]);
  543. ADD_TO_RESULT_PHIS(block, value, result_index);
  544. }
  545. /* Jump to end block */
  546. BUILD_BR(block->llvm_end_block);
  547. if (!block->skip_wasm_code_else && block->llvm_else_block) {
  548. /* Clear value stack, recover param values
  549. * and start to translate else branch.
  550. */
  551. aot_value_stack_destroy(&block->value_stack);
  552. for (i = 0; i < block->param_count; i++)
  553. PUSH(block->else_param_phis[i], block->param_types[i]);
  554. SET_BUILDER_POS(block->llvm_else_block);
  555. aot_checked_addr_list_destroy(func_ctx);
  556. return true;
  557. }
  558. /* No else branch or no need to translate else branch */
  559. block->is_reachable = true;
  560. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  561. fail:
  562. return false;
  563. }
  564. bool
  565. aot_compile_op_end(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  566. uint8 **p_frame_ip)
  567. {
  568. AOTBlock *block;
  569. LLVMValueRef value;
  570. LLVMBasicBlockRef next_llvm_end_block;
  571. char name[32];
  572. uint32 i, result_index;
  573. /* Check block stack */
  574. if (!(block = func_ctx->block_stack.block_list_end)) {
  575. aot_set_last_error("WASM block stack underflow.");
  576. return false;
  577. }
  578. /* Create the end block */
  579. if (!block->llvm_end_block) {
  580. format_block_name(name, sizeof(name), block->block_index,
  581. block->label_type, LABEL_END);
  582. CREATE_BLOCK(block->llvm_end_block, name);
  583. if ((next_llvm_end_block = find_next_llvm_end_block(block)))
  584. MOVE_BLOCK_BEFORE(block->llvm_end_block, next_llvm_end_block);
  585. }
  586. /* Handle block result values */
  587. CREATE_RESULT_VALUE_PHIS(block);
  588. for (i = 0; i < block->result_count; i++) {
  589. value = NULL;
  590. result_index = block->result_count - 1 - i;
  591. POP(value, block->result_types[result_index]);
  592. bh_assert(value);
  593. ADD_TO_RESULT_PHIS(block, value, result_index);
  594. }
  595. /* Jump to the end block */
  596. BUILD_BR(block->llvm_end_block);
  597. block->is_reachable = true;
  598. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  599. fail:
  600. return false;
  601. }
  602. #if WASM_ENABLE_THREAD_MGR != 0
  603. bool
  604. check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  605. {
  606. LLVMValueRef terminate_addr, terminate_flags, flag, offset, res;
  607. LLVMBasicBlockRef terminate_block, non_terminate_block;
  608. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  609. bool is_shared_memory =
  610. comp_ctx->comp_data->memories[0].memory_flags & 0x02 ? true : false;
  611. /* Only need to check the suspend flags when memory is shared since
  612. shared memory must be enabled for multi-threading */
  613. if (!is_shared_memory) {
  614. return true;
  615. }
  616. /* Offset of suspend_flags */
  617. offset = I32_FIVE;
  618. if (!(terminate_addr = LLVMBuildInBoundsGEP2(
  619. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->exec_env, &offset, 1,
  620. "terminate_addr"))) {
  621. aot_set_last_error("llvm build in bounds gep failed");
  622. return false;
  623. }
  624. if (!(terminate_addr =
  625. LLVMBuildBitCast(comp_ctx->builder, terminate_addr,
  626. INT32_PTR_TYPE, "terminate_addr_ptr"))) {
  627. aot_set_last_error("llvm build bit cast failed");
  628. return false;
  629. }
  630. if (!(terminate_flags =
  631. LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, terminate_addr,
  632. "terminate_flags"))) {
  633. aot_set_last_error("llvm build LOAD failed");
  634. return false;
  635. }
  636. /* Set terminate_flags memory accecc to volatile, so that the value
  637. will always be loaded from memory rather than register */
  638. LLVMSetVolatile(terminate_flags, true);
  639. if (!(flag = LLVMBuildAnd(comp_ctx->builder, terminate_flags, I32_ONE,
  640. "termination_flag"))) {
  641. aot_set_last_error("llvm build AND failed");
  642. return false;
  643. }
  644. CREATE_BLOCK(non_terminate_block, "non_terminate");
  645. MOVE_BLOCK_AFTER_CURR(non_terminate_block);
  646. CREATE_BLOCK(terminate_block, "terminate");
  647. MOVE_BLOCK_AFTER_CURR(terminate_block);
  648. BUILD_ICMP(LLVMIntEQ, flag, I32_ZERO, res, "flag_terminate");
  649. BUILD_COND_BR(res, non_terminate_block, terminate_block);
  650. /* Move builder to terminate block */
  651. SET_BUILDER_POS(terminate_block);
  652. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  653. goto fail;
  654. }
  655. /* Move builder to non terminate block */
  656. SET_BUILDER_POS(non_terminate_block);
  657. return true;
  658. fail:
  659. return false;
  660. }
  661. #endif /* End of WASM_ENABLE_THREAD_MGR */
  662. bool
  663. aot_compile_op_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  664. uint32 br_depth, uint8 **p_frame_ip)
  665. {
  666. AOTBlock *block_dst;
  667. LLVMValueRef value_ret, value_param;
  668. LLVMBasicBlockRef next_llvm_end_block;
  669. char name[32];
  670. uint32 i, param_index, result_index;
  671. #if WASM_ENABLE_THREAD_MGR != 0
  672. /* Insert suspend check point */
  673. if (comp_ctx->enable_thread_mgr) {
  674. if (!check_suspend_flags(comp_ctx, func_ctx))
  675. return false;
  676. }
  677. #endif
  678. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  679. return false;
  680. }
  681. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  682. /* Dest block is Loop block */
  683. /* Handle Loop parameters */
  684. for (i = 0; i < block_dst->param_count; i++) {
  685. param_index = block_dst->param_count - 1 - i;
  686. POP(value_param, block_dst->param_types[param_index]);
  687. ADD_TO_PARAM_PHIS(block_dst, value_param, param_index);
  688. }
  689. BUILD_BR(block_dst->llvm_entry_block);
  690. }
  691. else {
  692. /* Dest block is Block/If/Function block */
  693. /* Create the end block */
  694. if (!block_dst->llvm_end_block) {
  695. format_block_name(name, sizeof(name), block_dst->block_index,
  696. block_dst->label_type, LABEL_END);
  697. CREATE_BLOCK(block_dst->llvm_end_block, name);
  698. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  699. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  700. next_llvm_end_block);
  701. }
  702. block_dst->is_reachable = true;
  703. /* Handle result values */
  704. CREATE_RESULT_VALUE_PHIS(block_dst);
  705. for (i = 0; i < block_dst->result_count; i++) {
  706. result_index = block_dst->result_count - 1 - i;
  707. POP(value_ret, block_dst->result_types[result_index]);
  708. ADD_TO_RESULT_PHIS(block_dst, value_ret, result_index);
  709. }
  710. /* Jump to the end block */
  711. BUILD_BR(block_dst->llvm_end_block);
  712. }
  713. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  714. fail:
  715. return false;
  716. }
  717. bool
  718. aot_compile_op_br_if(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  719. uint32 br_depth, uint8 **p_frame_ip)
  720. {
  721. AOTBlock *block_dst;
  722. LLVMValueRef value_cmp, value, *values = NULL;
  723. LLVMBasicBlockRef llvm_else_block, next_llvm_end_block;
  724. char name[32];
  725. uint32 i, param_index, result_index;
  726. uint64 size;
  727. #if WASM_ENABLE_THREAD_MGR != 0
  728. /* Insert suspend check point */
  729. if (comp_ctx->enable_thread_mgr) {
  730. if (!check_suspend_flags(comp_ctx, func_ctx))
  731. return false;
  732. }
  733. #endif
  734. POP_COND(value_cmp);
  735. if (LLVMIsUndef(value_cmp)
  736. #if LLVM_VERSION_NUMBER >= 12
  737. || LLVMIsPoison(value_cmp)
  738. #endif
  739. ) {
  740. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  741. false, NULL, NULL))) {
  742. goto fail;
  743. }
  744. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  745. }
  746. if (!LLVMIsConstant(value_cmp)) {
  747. /* Compare value is not constant, create condition br IR */
  748. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  749. return false;
  750. }
  751. /* Create llvm else block */
  752. CREATE_BLOCK(llvm_else_block, "br_if_else");
  753. MOVE_BLOCK_AFTER_CURR(llvm_else_block);
  754. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  755. /* Dest block is Loop block */
  756. /* Handle Loop parameters */
  757. if (block_dst->param_count) {
  758. size = sizeof(LLVMValueRef) * (uint64)block_dst->param_count;
  759. if (size >= UINT32_MAX
  760. || !(values = wasm_runtime_malloc((uint32)size))) {
  761. aot_set_last_error("allocate memory failed.");
  762. goto fail;
  763. }
  764. for (i = 0; i < block_dst->param_count; i++) {
  765. param_index = block_dst->param_count - 1 - i;
  766. POP(value, block_dst->param_types[param_index]);
  767. ADD_TO_PARAM_PHIS(block_dst, value, param_index);
  768. values[param_index] = value;
  769. }
  770. for (i = 0; i < block_dst->param_count; i++) {
  771. PUSH(values[i], block_dst->param_types[i]);
  772. }
  773. wasm_runtime_free(values);
  774. values = NULL;
  775. }
  776. BUILD_COND_BR(value_cmp, block_dst->llvm_entry_block,
  777. llvm_else_block);
  778. /* Move builder to else block */
  779. SET_BUILDER_POS(llvm_else_block);
  780. }
  781. else {
  782. /* Dest block is Block/If/Function block */
  783. /* Create the end block */
  784. if (!block_dst->llvm_end_block) {
  785. format_block_name(name, sizeof(name), block_dst->block_index,
  786. block_dst->label_type, LABEL_END);
  787. CREATE_BLOCK(block_dst->llvm_end_block, name);
  788. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  789. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  790. next_llvm_end_block);
  791. }
  792. /* Set reachable flag and create condition br IR */
  793. block_dst->is_reachable = true;
  794. /* Handle result values */
  795. if (block_dst->result_count) {
  796. size = sizeof(LLVMValueRef) * (uint64)block_dst->result_count;
  797. if (size >= UINT32_MAX
  798. || !(values = wasm_runtime_malloc((uint32)size))) {
  799. aot_set_last_error("allocate memory failed.");
  800. goto fail;
  801. }
  802. CREATE_RESULT_VALUE_PHIS(block_dst);
  803. for (i = 0; i < block_dst->result_count; i++) {
  804. result_index = block_dst->result_count - 1 - i;
  805. POP(value, block_dst->result_types[result_index]);
  806. values[result_index] = value;
  807. ADD_TO_RESULT_PHIS(block_dst, value, result_index);
  808. }
  809. for (i = 0; i < block_dst->result_count; i++) {
  810. PUSH(values[i], block_dst->result_types[i]);
  811. }
  812. wasm_runtime_free(values);
  813. values = NULL;
  814. }
  815. /* Condition jump to end block */
  816. BUILD_COND_BR(value_cmp, block_dst->llvm_end_block,
  817. llvm_else_block);
  818. /* Move builder to else block */
  819. SET_BUILDER_POS(llvm_else_block);
  820. }
  821. }
  822. else {
  823. if ((int32)LLVMConstIntGetZExtValue(value_cmp) != 0) {
  824. /* Compare value is not 0, condition is true, same as op_br */
  825. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  826. }
  827. else {
  828. /* Compare value is not 0, condition is false, skip br_if */
  829. return true;
  830. }
  831. }
  832. return true;
  833. fail:
  834. if (values)
  835. wasm_runtime_free(values);
  836. return false;
  837. }
  838. bool
  839. aot_compile_op_br_table(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  840. uint32 *br_depths, uint32 br_count, uint8 **p_frame_ip)
  841. {
  842. uint32 i, j;
  843. LLVMValueRef value_switch, value_cmp, value_case, value, *values = NULL;
  844. LLVMBasicBlockRef default_llvm_block = NULL, target_llvm_block;
  845. LLVMBasicBlockRef next_llvm_end_block;
  846. AOTBlock *target_block;
  847. uint32 br_depth, depth_idx;
  848. uint32 param_index, result_index;
  849. uint64 size;
  850. char name[32];
  851. #if WASM_ENABLE_THREAD_MGR != 0
  852. /* Insert suspend check point */
  853. if (comp_ctx->enable_thread_mgr) {
  854. if (!check_suspend_flags(comp_ctx, func_ctx))
  855. return false;
  856. }
  857. #endif
  858. POP_I32(value_cmp);
  859. if (LLVMIsUndef(value_cmp)
  860. #if LLVM_VERSION_NUMBER >= 12
  861. || LLVMIsPoison(value_cmp)
  862. #endif
  863. ) {
  864. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  865. false, NULL, NULL))) {
  866. goto fail;
  867. }
  868. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  869. }
  870. if (!LLVMIsConstant(value_cmp)) {
  871. /* Compare value is not constant, create switch IR */
  872. for (i = 0; i <= br_count; i++) {
  873. target_block = get_target_block(func_ctx, br_depths[i]);
  874. if (!target_block)
  875. return false;
  876. if (target_block->label_type != LABEL_TYPE_LOOP) {
  877. /* Dest block is Block/If/Function block */
  878. /* Create the end block */
  879. if (!target_block->llvm_end_block) {
  880. format_block_name(name, sizeof(name),
  881. target_block->block_index,
  882. target_block->label_type, LABEL_END);
  883. CREATE_BLOCK(target_block->llvm_end_block, name);
  884. if ((next_llvm_end_block =
  885. find_next_llvm_end_block(target_block)))
  886. MOVE_BLOCK_BEFORE(target_block->llvm_end_block,
  887. next_llvm_end_block);
  888. }
  889. /* Handle result values */
  890. if (target_block->result_count) {
  891. size = sizeof(LLVMValueRef)
  892. * (uint64)target_block->result_count;
  893. if (size >= UINT32_MAX
  894. || !(values = wasm_runtime_malloc((uint32)size))) {
  895. aot_set_last_error("allocate memory failed.");
  896. goto fail;
  897. }
  898. CREATE_RESULT_VALUE_PHIS(target_block);
  899. for (j = 0; j < target_block->result_count; j++) {
  900. result_index = target_block->result_count - 1 - j;
  901. POP(value, target_block->result_types[result_index]);
  902. values[result_index] = value;
  903. ADD_TO_RESULT_PHIS(target_block, value, result_index);
  904. }
  905. for (j = 0; j < target_block->result_count; j++) {
  906. PUSH(values[j], target_block->result_types[j]);
  907. }
  908. wasm_runtime_free(values);
  909. }
  910. target_block->is_reachable = true;
  911. if (i == br_count)
  912. default_llvm_block = target_block->llvm_end_block;
  913. }
  914. else {
  915. /* Handle Loop parameters */
  916. if (target_block->param_count) {
  917. size = sizeof(LLVMValueRef)
  918. * (uint64)target_block->param_count;
  919. if (size >= UINT32_MAX
  920. || !(values = wasm_runtime_malloc((uint32)size))) {
  921. aot_set_last_error("allocate memory failed.");
  922. goto fail;
  923. }
  924. for (j = 0; j < target_block->param_count; j++) {
  925. param_index = target_block->param_count - 1 - j;
  926. POP(value, target_block->param_types[param_index]);
  927. values[param_index] = value;
  928. ADD_TO_PARAM_PHIS(target_block, value, param_index);
  929. }
  930. for (j = 0; j < target_block->param_count; j++) {
  931. PUSH(values[j], target_block->param_types[j]);
  932. }
  933. wasm_runtime_free(values);
  934. }
  935. if (i == br_count)
  936. default_llvm_block = target_block->llvm_entry_block;
  937. }
  938. }
  939. /* Create switch IR */
  940. if (!(value_switch = LLVMBuildSwitch(comp_ctx->builder, value_cmp,
  941. default_llvm_block, br_count))) {
  942. aot_set_last_error("llvm build switch failed.");
  943. return false;
  944. }
  945. /* Add each case for switch IR */
  946. for (i = 0; i < br_count; i++) {
  947. value_case = I32_CONST(i);
  948. CHECK_LLVM_CONST(value_case);
  949. target_block = get_target_block(func_ctx, br_depths[i]);
  950. if (!target_block)
  951. return false;
  952. target_llvm_block = target_block->label_type != LABEL_TYPE_LOOP
  953. ? target_block->llvm_end_block
  954. : target_block->llvm_entry_block;
  955. LLVMAddCase(value_switch, value_case, target_llvm_block);
  956. }
  957. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  958. }
  959. else {
  960. /* Compare value is constant, create br IR */
  961. depth_idx = (uint32)LLVMConstIntGetZExtValue(value_cmp);
  962. br_depth = br_depths[br_count];
  963. if (depth_idx < br_count) {
  964. br_depth = br_depths[depth_idx];
  965. }
  966. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  967. }
  968. fail:
  969. if (values)
  970. wasm_runtime_free(values);
  971. return false;
  972. }
  973. bool
  974. aot_compile_op_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  975. uint8 **p_frame_ip)
  976. {
  977. AOTBlock *block_func = func_ctx->block_stack.block_list_head;
  978. LLVMValueRef value;
  979. LLVMValueRef ret;
  980. AOTFuncType *func_type;
  981. uint32 i, param_index, result_index;
  982. #if WASM_ENABLE_DEBUG_AOT != 0
  983. LLVMMetadataRef return_location;
  984. #endif
  985. bh_assert(block_func);
  986. func_type = func_ctx->aot_func->func_type;
  987. #if WASM_ENABLE_DEBUG_AOT != 0
  988. return_location = dwarf_gen_location(
  989. comp_ctx, func_ctx,
  990. (*p_frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  991. #endif
  992. if (block_func->result_count) {
  993. /* Store extra result values to function parameters */
  994. for (i = 0; i < block_func->result_count - 1; i++) {
  995. result_index = block_func->result_count - 1 - i;
  996. POP(value, block_func->result_types[result_index]);
  997. param_index = func_type->param_count + result_index;
  998. if (!LLVMBuildStore(comp_ctx->builder, value,
  999. LLVMGetParam(func_ctx->func, param_index))) {
  1000. aot_set_last_error("llvm build store failed.");
  1001. goto fail;
  1002. }
  1003. }
  1004. /* Return the first result value */
  1005. POP(value, block_func->result_types[0]);
  1006. if (!(ret = LLVMBuildRet(comp_ctx->builder, value))) {
  1007. aot_set_last_error("llvm build return failed.");
  1008. goto fail;
  1009. }
  1010. #if WASM_ENABLE_DEBUG_AOT != 0
  1011. LLVMInstructionSetDebugLoc(ret, return_location);
  1012. #endif
  1013. }
  1014. else {
  1015. if (!(ret = LLVMBuildRetVoid(comp_ctx->builder))) {
  1016. aot_set_last_error("llvm build return void failed.");
  1017. goto fail;
  1018. }
  1019. #if WASM_ENABLE_DEBUG_AOT != 0
  1020. LLVMInstructionSetDebugLoc(ret, return_location);
  1021. #endif
  1022. }
  1023. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1024. fail:
  1025. return false;
  1026. }
  1027. bool
  1028. aot_compile_op_unreachable(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1029. uint8 **p_frame_ip)
  1030. {
  1031. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNREACHABLE, false, NULL,
  1032. NULL))
  1033. return false;
  1034. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1035. }
  1036. bool
  1037. aot_handle_next_reachable_block(AOTCompContext *comp_ctx,
  1038. AOTFuncContext *func_ctx, uint8 **p_frame_ip)
  1039. {
  1040. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1041. }