aot_emit_control.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. return aot_handle_next_reachable_block(comp_ctx, func_ctx,
  418. p_frame_ip);
  419. }
  420. if (!LLVMIsConstant(value)) {
  421. /* Compare value is not constant, create condition br IR */
  422. /* Create entry block */
  423. format_block_name(name, sizeof(name), block->block_index,
  424. label_type, LABEL_BEGIN);
  425. CREATE_BLOCK(block->llvm_entry_block, name);
  426. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  427. /* Create end block */
  428. format_block_name(name, sizeof(name), block->block_index,
  429. label_type, LABEL_END);
  430. CREATE_BLOCK(block->llvm_end_block, name);
  431. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_entry_block);
  432. if (else_addr) {
  433. /* Create else block */
  434. format_block_name(name, sizeof(name), block->block_index,
  435. label_type, LABEL_ELSE);
  436. CREATE_BLOCK(block->llvm_else_block, name);
  437. MOVE_BLOCK_AFTER(block->llvm_else_block,
  438. block->llvm_entry_block);
  439. /* Create condition br IR */
  440. BUILD_COND_BR(value, block->llvm_entry_block,
  441. block->llvm_else_block);
  442. }
  443. else {
  444. /* Create condition br IR */
  445. BUILD_COND_BR(value, block->llvm_entry_block,
  446. block->llvm_end_block);
  447. block->is_reachable = true;
  448. }
  449. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  450. block))
  451. goto fail;
  452. /* Start to translate if branch of BLOCK if */
  453. SET_BUILDER_POS(block->llvm_entry_block);
  454. }
  455. else {
  456. if ((int32)LLVMConstIntGetZExtValue(value) != 0) {
  457. /* Compare value is not 0, condition is true, else branch of
  458. BLOCK if cannot be reached */
  459. block->skip_wasm_code_else = true;
  460. /* Create entry block */
  461. format_block_name(name, sizeof(name), block->block_index,
  462. label_type, LABEL_BEGIN);
  463. CREATE_BLOCK(block->llvm_entry_block, name);
  464. MOVE_BLOCK_AFTER_CURR(block->llvm_entry_block);
  465. /* Jump to the entry block */
  466. BUILD_BR(block->llvm_entry_block);
  467. if (!push_aot_block_to_stack_and_pass_params(comp_ctx, func_ctx,
  468. block))
  469. goto fail;
  470. /* Start to translate the if branch */
  471. SET_BUILDER_POS(block->llvm_entry_block);
  472. }
  473. else {
  474. /* Compare value is not 0, condition is false, if branch of
  475. BLOCK if cannot be reached */
  476. if (else_addr) {
  477. /* Create else block */
  478. format_block_name(name, sizeof(name), block->block_index,
  479. label_type, LABEL_ELSE);
  480. CREATE_BLOCK(block->llvm_else_block, name);
  481. MOVE_BLOCK_AFTER_CURR(block->llvm_else_block);
  482. /* Jump to the else block */
  483. BUILD_BR(block->llvm_else_block);
  484. if (!push_aot_block_to_stack_and_pass_params(
  485. comp_ctx, func_ctx, block))
  486. goto fail;
  487. /* Start to translate the else branch */
  488. SET_BUILDER_POS(block->llvm_else_block);
  489. *p_frame_ip = else_addr + 1;
  490. }
  491. else {
  492. /* skip the block */
  493. aot_block_destroy(block);
  494. *p_frame_ip = end_addr + 1;
  495. }
  496. }
  497. }
  498. }
  499. else {
  500. aot_set_last_error("Invalid block type.");
  501. goto fail;
  502. }
  503. return true;
  504. fail:
  505. aot_block_destroy(block);
  506. return false;
  507. }
  508. bool
  509. aot_compile_op_else(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  510. uint8 **p_frame_ip)
  511. {
  512. AOTBlock *block = func_ctx->block_stack.block_list_end;
  513. LLVMValueRef value;
  514. char name[32];
  515. uint32 i, result_index;
  516. /* Check block */
  517. if (!block) {
  518. aot_set_last_error("WASM block stack underflow.");
  519. return false;
  520. }
  521. if (block->label_type != LABEL_TYPE_IF
  522. || (!block->skip_wasm_code_else && !block->llvm_else_block)) {
  523. aot_set_last_error("Invalid WASM block type.");
  524. return false;
  525. }
  526. /* Create end block if needed */
  527. if (!block->llvm_end_block) {
  528. format_block_name(name, sizeof(name), block->block_index,
  529. block->label_type, LABEL_END);
  530. CREATE_BLOCK(block->llvm_end_block, name);
  531. if (block->llvm_else_block)
  532. MOVE_BLOCK_AFTER(block->llvm_end_block, block->llvm_else_block);
  533. else
  534. MOVE_BLOCK_AFTER_CURR(block->llvm_end_block);
  535. }
  536. block->is_reachable = true;
  537. /* Comes from the if branch of BLOCK if */
  538. CREATE_RESULT_VALUE_PHIS(block);
  539. for (i = 0; i < block->result_count; i++) {
  540. result_index = block->result_count - 1 - i;
  541. POP(value, block->result_types[result_index]);
  542. ADD_TO_RESULT_PHIS(block, value, result_index);
  543. }
  544. /* Jump to end block */
  545. BUILD_BR(block->llvm_end_block);
  546. if (!block->skip_wasm_code_else && block->llvm_else_block) {
  547. /* Clear value stack, recover param values
  548. * and start to translate else branch.
  549. */
  550. aot_value_stack_destroy(&block->value_stack);
  551. for (i = 0; i < block->param_count; i++)
  552. PUSH(block->else_param_phis[i], block->param_types[i]);
  553. SET_BUILDER_POS(block->llvm_else_block);
  554. aot_checked_addr_list_destroy(func_ctx);
  555. return true;
  556. }
  557. /* No else branch or no need to translate else branch */
  558. block->is_reachable = true;
  559. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  560. fail:
  561. return false;
  562. }
  563. bool
  564. aot_compile_op_end(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  565. uint8 **p_frame_ip)
  566. {
  567. AOTBlock *block;
  568. LLVMValueRef value;
  569. LLVMBasicBlockRef next_llvm_end_block;
  570. char name[32];
  571. uint32 i, result_index;
  572. /* Check block stack */
  573. if (!(block = func_ctx->block_stack.block_list_end)) {
  574. aot_set_last_error("WASM block stack underflow.");
  575. return false;
  576. }
  577. /* Create the end block */
  578. if (!block->llvm_end_block) {
  579. format_block_name(name, sizeof(name), block->block_index,
  580. block->label_type, LABEL_END);
  581. CREATE_BLOCK(block->llvm_end_block, name);
  582. if ((next_llvm_end_block = find_next_llvm_end_block(block)))
  583. MOVE_BLOCK_BEFORE(block->llvm_end_block, next_llvm_end_block);
  584. }
  585. /* Handle block result values */
  586. CREATE_RESULT_VALUE_PHIS(block);
  587. for (i = 0; i < block->result_count; i++) {
  588. value = NULL;
  589. result_index = block->result_count - 1 - i;
  590. POP(value, block->result_types[result_index]);
  591. bh_assert(value);
  592. ADD_TO_RESULT_PHIS(block, value, result_index);
  593. }
  594. /* Jump to the end block */
  595. BUILD_BR(block->llvm_end_block);
  596. block->is_reachable = true;
  597. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  598. fail:
  599. return false;
  600. }
  601. #if WASM_ENABLE_THREAD_MGR != 0
  602. bool
  603. check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  604. {
  605. LLVMValueRef terminate_addr, terminate_flags, flag, offset, res;
  606. LLVMBasicBlockRef terminate_check_block, non_terminate_block;
  607. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  608. LLVMBasicBlockRef terminate_block;
  609. /* Offset of suspend_flags */
  610. offset = I32_FIVE;
  611. if (!(terminate_addr = LLVMBuildInBoundsGEP2(
  612. comp_ctx->builder, OPQ_PTR_TYPE, func_ctx->exec_env, &offset, 1,
  613. "terminate_addr"))) {
  614. aot_set_last_error("llvm build in bounds gep failed");
  615. return false;
  616. }
  617. if (!(terminate_addr =
  618. LLVMBuildBitCast(comp_ctx->builder, terminate_addr,
  619. INT32_PTR_TYPE, "terminate_addr_ptr"))) {
  620. aot_set_last_error("llvm build bit cast failed");
  621. return false;
  622. }
  623. if (!(terminate_flags =
  624. LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, terminate_addr,
  625. "terminate_flags"))) {
  626. aot_set_last_error("llvm build bit cast failed");
  627. return false;
  628. }
  629. /* Set terminate_flags memory accecc to volatile, so that the value
  630. will always be loaded from memory rather than register */
  631. LLVMSetVolatile(terminate_flags, true);
  632. CREATE_BLOCK(terminate_check_block, "terminate_check");
  633. MOVE_BLOCK_AFTER_CURR(terminate_check_block);
  634. CREATE_BLOCK(non_terminate_block, "non_terminate");
  635. MOVE_BLOCK_AFTER_CURR(non_terminate_block);
  636. BUILD_ICMP(LLVMIntSGT, terminate_flags, I32_ZERO, res, "need_terminate");
  637. BUILD_COND_BR(res, terminate_check_block, non_terminate_block);
  638. /* Move builder to terminate check block */
  639. SET_BUILDER_POS(terminate_check_block);
  640. CREATE_BLOCK(terminate_block, "terminate");
  641. MOVE_BLOCK_AFTER_CURR(terminate_block);
  642. if (!(flag = LLVMBuildAnd(comp_ctx->builder, terminate_flags, I32_ONE,
  643. "termination_flag"))) {
  644. aot_set_last_error("llvm build AND failed");
  645. return false;
  646. }
  647. BUILD_ICMP(LLVMIntSGT, flag, I32_ZERO, res, "need_terminate");
  648. BUILD_COND_BR(res, terminate_block, non_terminate_block);
  649. /* Move builder to terminate block */
  650. SET_BUILDER_POS(terminate_block);
  651. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  652. goto fail;
  653. }
  654. /* Move builder to terminate block */
  655. SET_BUILDER_POS(non_terminate_block);
  656. return true;
  657. fail:
  658. return false;
  659. }
  660. #endif /* End of WASM_ENABLE_THREAD_MGR */
  661. bool
  662. aot_compile_op_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  663. uint32 br_depth, uint8 **p_frame_ip)
  664. {
  665. AOTBlock *block_dst;
  666. LLVMValueRef value_ret, value_param;
  667. LLVMBasicBlockRef next_llvm_end_block;
  668. char name[32];
  669. uint32 i, param_index, result_index;
  670. #if WASM_ENABLE_THREAD_MGR != 0
  671. /* Insert suspend check point */
  672. if (comp_ctx->enable_thread_mgr) {
  673. if (!check_suspend_flags(comp_ctx, func_ctx))
  674. return false;
  675. }
  676. #endif
  677. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  678. return false;
  679. }
  680. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  681. /* Dest block is Loop block */
  682. /* Handle Loop parameters */
  683. for (i = 0; i < block_dst->param_count; i++) {
  684. param_index = block_dst->param_count - 1 - i;
  685. POP(value_param, block_dst->param_types[param_index]);
  686. ADD_TO_PARAM_PHIS(block_dst, value_param, param_index);
  687. }
  688. BUILD_BR(block_dst->llvm_entry_block);
  689. }
  690. else {
  691. /* Dest block is Block/If/Function block */
  692. /* Create the end block */
  693. if (!block_dst->llvm_end_block) {
  694. format_block_name(name, sizeof(name), block_dst->block_index,
  695. block_dst->label_type, LABEL_END);
  696. CREATE_BLOCK(block_dst->llvm_end_block, name);
  697. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  698. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  699. next_llvm_end_block);
  700. }
  701. block_dst->is_reachable = true;
  702. /* Handle result values */
  703. CREATE_RESULT_VALUE_PHIS(block_dst);
  704. for (i = 0; i < block_dst->result_count; i++) {
  705. result_index = block_dst->result_count - 1 - i;
  706. POP(value_ret, block_dst->result_types[result_index]);
  707. ADD_TO_RESULT_PHIS(block_dst, value_ret, result_index);
  708. }
  709. /* Jump to the end block */
  710. BUILD_BR(block_dst->llvm_end_block);
  711. }
  712. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  713. fail:
  714. return false;
  715. }
  716. bool
  717. aot_compile_op_br_if(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  718. uint32 br_depth, uint8 **p_frame_ip)
  719. {
  720. AOTBlock *block_dst;
  721. LLVMValueRef value_cmp, value, *values = NULL;
  722. LLVMBasicBlockRef llvm_else_block, next_llvm_end_block;
  723. char name[32];
  724. uint32 i, param_index, result_index;
  725. uint64 size;
  726. #if WASM_ENABLE_THREAD_MGR != 0
  727. /* Insert suspend check point */
  728. if (comp_ctx->enable_thread_mgr) {
  729. if (!check_suspend_flags(comp_ctx, func_ctx))
  730. return false;
  731. }
  732. #endif
  733. POP_COND(value_cmp);
  734. if (LLVMIsUndef(value_cmp)
  735. #if LLVM_VERSION_NUMBER >= 12
  736. || LLVMIsPoison(value_cmp)
  737. #endif
  738. ) {
  739. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  740. false, NULL, NULL))) {
  741. goto fail;
  742. }
  743. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  744. }
  745. if (!LLVMIsConstant(value_cmp)) {
  746. /* Compare value is not constant, create condition br IR */
  747. if (!(block_dst = get_target_block(func_ctx, br_depth))) {
  748. return false;
  749. }
  750. /* Create llvm else block */
  751. CREATE_BLOCK(llvm_else_block, "br_if_else");
  752. MOVE_BLOCK_AFTER_CURR(llvm_else_block);
  753. if (block_dst->label_type == LABEL_TYPE_LOOP) {
  754. /* Dest block is Loop block */
  755. /* Handle Loop parameters */
  756. if (block_dst->param_count) {
  757. size = sizeof(LLVMValueRef) * (uint64)block_dst->param_count;
  758. if (size >= UINT32_MAX
  759. || !(values = wasm_runtime_malloc((uint32)size))) {
  760. aot_set_last_error("allocate memory failed.");
  761. goto fail;
  762. }
  763. for (i = 0; i < block_dst->param_count; i++) {
  764. param_index = block_dst->param_count - 1 - i;
  765. POP(value, block_dst->param_types[param_index]);
  766. ADD_TO_PARAM_PHIS(block_dst, value, param_index);
  767. values[param_index] = value;
  768. }
  769. for (i = 0; i < block_dst->param_count; i++) {
  770. PUSH(values[i], block_dst->param_types[i]);
  771. }
  772. wasm_runtime_free(values);
  773. values = NULL;
  774. }
  775. BUILD_COND_BR(value_cmp, block_dst->llvm_entry_block,
  776. llvm_else_block);
  777. /* Move builder to else block */
  778. SET_BUILDER_POS(llvm_else_block);
  779. }
  780. else {
  781. /* Dest block is Block/If/Function block */
  782. /* Create the end block */
  783. if (!block_dst->llvm_end_block) {
  784. format_block_name(name, sizeof(name), block_dst->block_index,
  785. block_dst->label_type, LABEL_END);
  786. CREATE_BLOCK(block_dst->llvm_end_block, name);
  787. if ((next_llvm_end_block = find_next_llvm_end_block(block_dst)))
  788. MOVE_BLOCK_BEFORE(block_dst->llvm_end_block,
  789. next_llvm_end_block);
  790. }
  791. /* Set reachable flag and create condition br IR */
  792. block_dst->is_reachable = true;
  793. /* Handle result values */
  794. if (block_dst->result_count) {
  795. size = sizeof(LLVMValueRef) * (uint64)block_dst->result_count;
  796. if (size >= UINT32_MAX
  797. || !(values = wasm_runtime_malloc((uint32)size))) {
  798. aot_set_last_error("allocate memory failed.");
  799. goto fail;
  800. }
  801. CREATE_RESULT_VALUE_PHIS(block_dst);
  802. for (i = 0; i < block_dst->result_count; i++) {
  803. result_index = block_dst->result_count - 1 - i;
  804. POP(value, block_dst->result_types[result_index]);
  805. values[result_index] = value;
  806. ADD_TO_RESULT_PHIS(block_dst, value, result_index);
  807. }
  808. for (i = 0; i < block_dst->result_count; i++) {
  809. PUSH(values[i], block_dst->result_types[i]);
  810. }
  811. wasm_runtime_free(values);
  812. values = NULL;
  813. }
  814. /* Condition jump to end block */
  815. BUILD_COND_BR(value_cmp, block_dst->llvm_end_block,
  816. llvm_else_block);
  817. /* Move builder to else block */
  818. SET_BUILDER_POS(llvm_else_block);
  819. }
  820. }
  821. else {
  822. if ((int32)LLVMConstIntGetZExtValue(value_cmp) != 0) {
  823. /* Compare value is not 0, condition is true, same as op_br */
  824. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  825. }
  826. else {
  827. /* Compare value is not 0, condition is false, skip br_if */
  828. return true;
  829. }
  830. }
  831. return true;
  832. fail:
  833. if (values)
  834. wasm_runtime_free(values);
  835. return false;
  836. }
  837. bool
  838. aot_compile_op_br_table(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  839. uint32 *br_depths, uint32 br_count, uint8 **p_frame_ip)
  840. {
  841. uint32 i, j;
  842. LLVMValueRef value_switch, value_cmp, value_case, value, *values = NULL;
  843. LLVMBasicBlockRef default_llvm_block = NULL, target_llvm_block;
  844. LLVMBasicBlockRef next_llvm_end_block;
  845. AOTBlock *target_block;
  846. uint32 br_depth, depth_idx;
  847. uint32 param_index, result_index;
  848. uint64 size;
  849. char name[32];
  850. #if WASM_ENABLE_THREAD_MGR != 0
  851. /* Insert suspend check point */
  852. if (comp_ctx->enable_thread_mgr) {
  853. if (!check_suspend_flags(comp_ctx, func_ctx))
  854. return false;
  855. }
  856. #endif
  857. POP_I32(value_cmp);
  858. if (LLVMIsUndef(value_cmp)
  859. #if LLVM_VERSION_NUMBER >= 12
  860. || LLVMIsPoison(value_cmp)
  861. #endif
  862. ) {
  863. if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
  864. false, NULL, NULL))) {
  865. goto fail;
  866. }
  867. return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  868. }
  869. if (!LLVMIsConstant(value_cmp)) {
  870. /* Compare value is not constant, create switch IR */
  871. for (i = 0; i <= br_count; i++) {
  872. target_block = get_target_block(func_ctx, br_depths[i]);
  873. if (!target_block)
  874. return false;
  875. if (target_block->label_type != LABEL_TYPE_LOOP) {
  876. /* Dest block is Block/If/Function block */
  877. /* Create the end block */
  878. if (!target_block->llvm_end_block) {
  879. format_block_name(name, sizeof(name),
  880. target_block->block_index,
  881. target_block->label_type, LABEL_END);
  882. CREATE_BLOCK(target_block->llvm_end_block, name);
  883. if ((next_llvm_end_block =
  884. find_next_llvm_end_block(target_block)))
  885. MOVE_BLOCK_BEFORE(target_block->llvm_end_block,
  886. next_llvm_end_block);
  887. }
  888. /* Handle result values */
  889. if (target_block->result_count) {
  890. size = sizeof(LLVMValueRef)
  891. * (uint64)target_block->result_count;
  892. if (size >= UINT32_MAX
  893. || !(values = wasm_runtime_malloc((uint32)size))) {
  894. aot_set_last_error("allocate memory failed.");
  895. goto fail;
  896. }
  897. CREATE_RESULT_VALUE_PHIS(target_block);
  898. for (j = 0; j < target_block->result_count; j++) {
  899. result_index = target_block->result_count - 1 - j;
  900. POP(value, target_block->result_types[result_index]);
  901. values[result_index] = value;
  902. ADD_TO_RESULT_PHIS(target_block, value, result_index);
  903. }
  904. for (j = 0; j < target_block->result_count; j++) {
  905. PUSH(values[j], target_block->result_types[j]);
  906. }
  907. wasm_runtime_free(values);
  908. }
  909. target_block->is_reachable = true;
  910. if (i == br_count)
  911. default_llvm_block = target_block->llvm_end_block;
  912. }
  913. else {
  914. /* Handle Loop parameters */
  915. if (target_block->param_count) {
  916. size = sizeof(LLVMValueRef)
  917. * (uint64)target_block->param_count;
  918. if (size >= UINT32_MAX
  919. || !(values = wasm_runtime_malloc((uint32)size))) {
  920. aot_set_last_error("allocate memory failed.");
  921. goto fail;
  922. }
  923. for (j = 0; j < target_block->param_count; j++) {
  924. param_index = target_block->param_count - 1 - j;
  925. POP(value, target_block->param_types[param_index]);
  926. values[param_index] = value;
  927. ADD_TO_PARAM_PHIS(target_block, value, param_index);
  928. }
  929. for (j = 0; j < target_block->param_count; j++) {
  930. PUSH(values[j], target_block->param_types[j]);
  931. }
  932. wasm_runtime_free(values);
  933. }
  934. if (i == br_count)
  935. default_llvm_block = target_block->llvm_entry_block;
  936. }
  937. }
  938. /* Create switch IR */
  939. if (!(value_switch = LLVMBuildSwitch(comp_ctx->builder, value_cmp,
  940. default_llvm_block, br_count))) {
  941. aot_set_last_error("llvm build switch failed.");
  942. return false;
  943. }
  944. /* Add each case for switch IR */
  945. for (i = 0; i < br_count; i++) {
  946. value_case = I32_CONST(i);
  947. CHECK_LLVM_CONST(value_case);
  948. target_block = get_target_block(func_ctx, br_depths[i]);
  949. if (!target_block)
  950. return false;
  951. target_llvm_block = target_block->label_type != LABEL_TYPE_LOOP
  952. ? target_block->llvm_end_block
  953. : target_block->llvm_entry_block;
  954. LLVMAddCase(value_switch, value_case, target_llvm_block);
  955. }
  956. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  957. }
  958. else {
  959. /* Compare value is constant, create br IR */
  960. depth_idx = (uint32)LLVMConstIntGetZExtValue(value_cmp);
  961. br_depth = br_depths[br_count];
  962. if (depth_idx < br_count) {
  963. br_depth = br_depths[depth_idx];
  964. }
  965. return aot_compile_op_br(comp_ctx, func_ctx, br_depth, p_frame_ip);
  966. }
  967. fail:
  968. if (values)
  969. wasm_runtime_free(values);
  970. return false;
  971. }
  972. bool
  973. aot_compile_op_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  974. uint8 **p_frame_ip)
  975. {
  976. AOTBlock *block_func = func_ctx->block_stack.block_list_head;
  977. LLVMValueRef value;
  978. LLVMValueRef ret;
  979. AOTFuncType *func_type;
  980. uint32 i, param_index, result_index;
  981. #if WASM_ENABLE_DEBUG_AOT != 0
  982. LLVMMetadataRef return_location;
  983. #endif
  984. bh_assert(block_func);
  985. func_type = func_ctx->aot_func->func_type;
  986. #if WASM_ENABLE_DEBUG_AOT != 0
  987. return_location = dwarf_gen_location(
  988. comp_ctx, func_ctx,
  989. (*p_frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  990. #endif
  991. if (block_func->result_count) {
  992. /* Store extra result values to function parameters */
  993. for (i = 0; i < block_func->result_count - 1; i++) {
  994. result_index = block_func->result_count - 1 - i;
  995. POP(value, block_func->result_types[result_index]);
  996. param_index = func_type->param_count + result_index;
  997. if (!LLVMBuildStore(comp_ctx->builder, value,
  998. LLVMGetParam(func_ctx->func, param_index))) {
  999. aot_set_last_error("llvm build store failed.");
  1000. goto fail;
  1001. }
  1002. }
  1003. /* Return the first result value */
  1004. POP(value, block_func->result_types[0]);
  1005. if (!(ret = LLVMBuildRet(comp_ctx->builder, value))) {
  1006. aot_set_last_error("llvm build return failed.");
  1007. goto fail;
  1008. }
  1009. #if WASM_ENABLE_DEBUG_AOT != 0
  1010. LLVMInstructionSetDebugLoc(ret, return_location);
  1011. #endif
  1012. }
  1013. else {
  1014. if (!(ret = LLVMBuildRetVoid(comp_ctx->builder))) {
  1015. aot_set_last_error("llvm build return void failed.");
  1016. goto fail;
  1017. }
  1018. #if WASM_ENABLE_DEBUG_AOT != 0
  1019. LLVMInstructionSetDebugLoc(ret, return_location);
  1020. #endif
  1021. }
  1022. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1023. fail:
  1024. return false;
  1025. }
  1026. bool
  1027. aot_compile_op_unreachable(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1028. uint8 **p_frame_ip)
  1029. {
  1030. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNREACHABLE, false, NULL,
  1031. NULL))
  1032. return false;
  1033. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1034. }
  1035. bool
  1036. aot_handle_next_reachable_block(AOTCompContext *comp_ctx,
  1037. AOTFuncContext *func_ctx, uint8 **p_frame_ip)
  1038. {
  1039. return handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
  1040. }