Skip to content

Commit 6523868

Browse files
fix aot load import global bug && enhance dead code processing (#203)
Co-authored-by: Xu Jun <[email protected]>
1 parent 2a74e2d commit 6523868

File tree

2 files changed

+33
-50
lines changed

2 files changed

+33
-50
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
614614

615615
/* Create each import global */
616616
for (i = 0; i < module->import_global_count; i++) {
617+
buf = (uint8*)align_ptr(buf, 2);
617618
read_uint8(buf, buf_end, import_globals[i].type);
618619
read_uint8(buf, buf_end, import_globals[i].is_mutable);
619620
read_string(buf, buf_end, import_globals[i].module_name);

core/iwasm/interpreter/wasm_loader.c

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,7 +2830,7 @@ add_label_patch_to_list(BranchBlock *frame_csp,
28302830

28312831
static void
28322832
apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
2833-
uint8 patch_type, uint8 *frame_ip)
2833+
uint8 patch_type)
28342834
{
28352835
BranchBlock *frame_csp = ctx->frame_csp - depth;
28362836
BranchBlockPatch *node = frame_csp->patch_list;
@@ -2882,7 +2882,12 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
28822882
char *error_buf, uint32 error_buf_size)
28832883
{
28842884
emit_operand(ctx, frame_csp->dynamic_offset);
2885-
if (frame_csp->return_type == VALUE_TYPE_I32
2885+
if (frame_csp->block_type == BLOCK_TYPE_LOOP ||
2886+
frame_csp->return_type == VALUE_TYPE_VOID) {
2887+
emit_byte(ctx, 0);
2888+
emit_operand(ctx, 0);
2889+
}
2890+
else if (frame_csp->return_type == VALUE_TYPE_I32
28862891
|| frame_csp->return_type == VALUE_TYPE_F32) {
28872892
emit_byte(ctx, 1);
28882893
emit_operand(ctx, *(int16*)(ctx->frame_offset - 1));
@@ -2892,10 +2897,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
28922897
emit_byte(ctx, 2);
28932898
emit_operand(ctx, *(int16*)(ctx->frame_offset - 2));
28942899
}
2895-
else {
2896-
emit_byte(ctx, 0);
2897-
emit_operand(ctx, 0);
2898-
}
2900+
28992901
if (frame_csp->block_type == BLOCK_TYPE_LOOP) {
29002902
wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
29012903
}
@@ -3514,10 +3516,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
35143516
error_buf, error_buf_size))
35153517
goto fail;
35163518

3517-
if ((loader_ctx->frame_csp - 1)->else_addr)
3518-
p = (loader_ctx->frame_csp - 1)->else_addr;
3519-
else
3519+
if ((loader_ctx->frame_csp - 1)->else_addr) {
3520+
#if WASM_ENABLE_FAST_INTERP != 0
3521+
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
3522+
(loader_ctx->frame_csp - 1)->stack_cell_num;
3523+
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
3524+
#endif
3525+
p = (loader_ctx->frame_csp - 1)->else_addr + 1;
3526+
}
3527+
else {
35203528
p = (loader_ctx->frame_csp - 1)->end_addr;
3529+
}
35213530

35223531
is_i32_const = false;
35233532
continue;
@@ -3539,7 +3548,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
35393548
loader_ctx->frame_ref = loader_ctx->frame_ref_bottom +
35403549
loader_ctx->stack_cell_num;
35413550
#if WASM_ENABLE_FAST_INTERP != 0
3542-
// if the result of if branch is in local or const area, add a copy op
3551+
/* if the result of if branch is in local or const area, add a copy op */
35433552
if ((loader_ctx->frame_csp - 1)->return_type != VALUE_TYPE_VOID) {
35443553
uint8 return_cells;
35453554
if ((loader_ctx->frame_csp - 1)->return_type == VALUE_TYPE_I32
@@ -3557,14 +3566,14 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
35573566
emit_operand(loader_ctx, *(loader_ctx->frame_offset - return_cells));
35583567
emit_operand(loader_ctx, (loader_ctx->frame_csp - 1)->dynamic_offset);
35593568
*(loader_ctx->frame_offset - return_cells) =
3560-
loader_ctx->frame_csp->dynamic_offset;
3569+
(loader_ctx->frame_csp - 1)->dynamic_offset;
35613570
emit_label(opcode);
35623571
}
35633572
}
35643573
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
35653574
loader_ctx->stack_cell_num;
35663575
emit_empty_label_addr_and_frame_ip(PATCH_END);
3567-
apply_label_patch(loader_ctx, 1, PATCH_ELSE, p);
3576+
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
35683577
#endif
35693578
break;
35703579

@@ -3601,7 +3610,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
36013610
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
36023611
}
36033612

3604-
apply_label_patch(loader_ctx, 0, PATCH_END, p);
3613+
apply_label_patch(loader_ctx, 0, PATCH_END);
36053614
free_label_patch_list(loader_ctx->frame_csp);
36063615
if (loader_ctx->frame_csp->block_type == BLOCK_TYPE_FUNCTION) {
36073616
emit_label(WASM_OP_RETURN);
@@ -3670,8 +3679,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
36703679

36713680
if ((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF
36723681
&& (loader_ctx->frame_csp - 1)->else_addr != NULL
3673-
&& p <= (loader_ctx->frame_csp - 1)->else_addr)
3674-
p = (loader_ctx->frame_csp - 1)->else_addr;
3682+
&& p <= (loader_ctx->frame_csp - 1)->else_addr) {
3683+
#if WASM_ENABLE_FAST_INTERP != 0
3684+
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
3685+
(loader_ctx->frame_csp - 1)->stack_cell_num;
3686+
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
3687+
#endif
3688+
p = (loader_ctx->frame_csp - 1)->else_addr + 1;
3689+
3690+
}
36753691
else {
36763692
p = (loader_ctx->frame_csp - 1)->end_addr;
36773693
PUSH_TYPE(block_return_type);
@@ -3736,40 +3752,6 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
37363752
POP_TYPE(ret_type);
37373753
PUSH_TYPE(ret_type);
37383754

3739-
cache_index = ((uintptr_t)(loader_ctx->frame_csp - 1)->start_addr)
3740-
& (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
3741-
cache_items = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * cache_index;
3742-
for (item_index = 0; item_index < BLOCK_ADDR_CONFLICT_SIZE;
3743-
item_index++) {
3744-
if (cache_items[item_index].start_addr ==
3745-
(loader_ctx->frame_csp - 1)->start_addr) {
3746-
(loader_ctx->frame_csp - 1)->else_addr = cache_items[item_index].else_addr;
3747-
(loader_ctx->frame_csp - 1)->end_addr = cache_items[item_index].end_addr;
3748-
break;
3749-
}
3750-
}
3751-
if(item_index == BLOCK_ADDR_CONFLICT_SIZE
3752-
&& !wasm_loader_find_block_addr(block_addr_cache,
3753-
(loader_ctx->frame_csp - 1)->start_addr,
3754-
p_end,
3755-
(loader_ctx->frame_csp - 1)->block_type,
3756-
&(loader_ctx->frame_csp - 1)->else_addr,
3757-
&(loader_ctx->frame_csp - 1)->end_addr,
3758-
error_buf, error_buf_size))
3759-
goto fail;
3760-
3761-
loader_ctx->stack_cell_num = (loader_ctx->frame_csp - 1)->stack_cell_num;
3762-
loader_ctx->frame_ref = loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num;
3763-
3764-
if ((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF
3765-
&& p <= (loader_ctx->frame_csp - 1)->else_addr) {
3766-
p = (loader_ctx->frame_csp - 1)->else_addr;
3767-
}
3768-
else {
3769-
p = (loader_ctx->frame_csp - 1)->end_addr;
3770-
PUSH_TYPE((loader_ctx->frame_csp - 1)->return_type);
3771-
}
3772-
37733755
#if WASM_ENABLE_FAST_INTERP != 0
37743756
// emit the offset after return opcode
37753757
POP_OFFSET_TYPE(ret_type);
@@ -3778,7 +3760,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
37783760
#endif
37793761

37803762
is_i32_const = false;
3781-
continue;
3763+
goto handle_next_reachable_block;
37823764
}
37833765

37843766
case WASM_OP_CALL:

0 commit comments

Comments
 (0)