Skip to content

Commit 591e4ce

Browse files
authored
Refine aot exception throw, remove unnecessary labels (#456)
1 parent 0700dc9 commit 591e4ce

File tree

4 files changed

+5
-75
lines changed

4 files changed

+5
-75
lines changed

core/iwasm/compilation/aot_compiler.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,13 +1770,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
17701770
if (last_block != func_ctx->got_exception_block)
17711771
LLVMMoveBasicBlockAfter(func_ctx->got_exception_block,
17721772
last_block);
1773-
1774-
/* Move all other exception blocks before got_exception block */
1775-
for (i = 0; i < EXCE_NUM; i++) {
1776-
if (func_ctx->exception_blocks[i])
1777-
LLVMMoveBasicBlockBefore(func_ctx->exception_blocks[i],
1778-
func_ctx->got_exception_block);
1779-
}
17801773
}
17811774
return true;
17821775

core/iwasm/compilation/aot_emit_exception.c

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,13 @@
66
#include "aot_emit_exception.h"
77
#include "../aot/aot_runtime.h"
88

9-
static char *exce_block_names[] = {
10-
"exce_unreachable", /* EXCE_UNREACHABLE */
11-
"exce_out_of_memory", /* EXCE_OUT_OF_MEMORY */
12-
"exce_out_of_bounds_mem_access",/* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
13-
"exce_integer_overflow", /* EXCE_INTEGER_OVERFLOW */
14-
"exce_divide_by_zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
15-
"exce_invalid_convert_to_int", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
16-
"exce_invalid_func_type_idx", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
17-
"exce_invalid_func_idx", /* EXCE_INVALID_FUNCTION_INDEX */
18-
"exce_undefined_element", /* EXCE_UNDEFINED_ELEMENT */
19-
"exce_uninit_element", /* EXCE_UNINITIALIZED_ELEMENT */
20-
"exce_call_unlinked", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
21-
"exce_native_stack_overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
22-
"exce_unaligned_atomic" /* EXCE_UNALIGNED_ATOMIC */
23-
};
24-
259
bool
2610
aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
2711
int32 exception_id,
2812
bool is_cond_br,
2913
LLVMValueRef cond_br_if,
3014
LLVMBasicBlockRef cond_br_else_block)
3115
{
32-
LLVMBasicBlockRef exce_block;
3316
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
3417
LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
3518
LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
@@ -116,44 +99,20 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
11699
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
117100
}
118101

119-
/* Create exception block if needed */
120-
if (!(exce_block = func_ctx->exception_blocks[exception_id])) {
121-
if (!(func_ctx->exception_blocks[exception_id] = exce_block =
122-
LLVMAppendBasicBlockInContext(comp_ctx->context,
123-
func_ctx->func,
124-
exce_block_names[exception_id]))) {
125-
aot_set_last_error("add LLVM basic block failed.");
126-
return false;
127-
}
128-
129-
/* Move before got_exception block */
130-
LLVMMoveBasicBlockBefore(exce_block, func_ctx->got_exception_block);
131-
132-
/* Add phi incoming value to got_exception block */
133-
LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &exce_block, 1);
134-
135-
/* Jump to got exception block */
136-
LLVMPositionBuilderAtEnd(comp_ctx->builder, exce_block);
137-
if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
138-
aot_set_last_error("llvm build br failed.");
139-
return false;
140-
}
141-
}
142-
143-
/* Resume builder position */
144-
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
102+
/* Add phi incoming value to got_exception block */
103+
LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &block_curr, 1);
145104

146105
if (!is_cond_br) {
147106
/* not condition br, create br IR */
148-
if (!LLVMBuildBr(comp_ctx->builder, exce_block)) {
107+
if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
149108
aot_set_last_error("llvm build br failed.");
150109
return false;
151110
}
152111
}
153112
else {
154113
/* Create condition br */
155114
if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
156-
exce_block, cond_br_else_block)) {
115+
func_ctx->got_exception_block, cond_br_else_block)) {
157116
aot_set_last_error("llvm build cond br failed.");
158117
return false;
159118
}

core/iwasm/compilation/aot_emit_function.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
9999
/* Add check exection success block */
100100
if (!(check_call_succ = LLVMAppendBasicBlockInContext(comp_ctx->context,
101101
func_ctx->func,
102-
"check_exce_succ"))) {
102+
"check_call_succ"))) {
103103
aot_set_last_error("llvm add basic block failed.");
104104
return false;
105105
}

core/iwasm/compilation/aot_llvm.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,6 @@ aot_create_func_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
172172
return NULL;
173173
}
174174

175-
static bool
176-
create_exception_blocks(AOTFuncContext *func_ctx)
177-
{
178-
if (!(func_ctx->exception_blocks =
179-
wasm_runtime_malloc(sizeof(LLVMBasicBlockRef) * EXCE_NUM))) {
180-
aot_set_last_error("allocate memory failed.");
181-
return false;;
182-
}
183-
184-
memset(func_ctx->exception_blocks, 0,
185-
sizeof(LLVMBasicBlockRef) * EXCE_NUM);
186-
return true;
187-
}
188-
189175
static bool
190176
create_memory_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
191177
LLVMTypeRef int8_ptr_type, uint32 func_index)
@@ -744,10 +730,6 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
744730
goto fail;
745731
}
746732

747-
/* Create exception blocks */
748-
if (!create_exception_blocks(func_ctx))
749-
goto fail;
750-
751733
/* Create base addr, end addr, data size of mem, heap */
752734
if (!create_memory_info(comp_ctx, func_ctx, int8_ptr_type, func_index))
753735
goto fail;
@@ -769,8 +751,6 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
769751
fail:
770752
if (func_ctx->mem_info)
771753
wasm_runtime_free(func_ctx->mem_info);
772-
if (func_ctx->exception_blocks)
773-
wasm_runtime_free(func_ctx->exception_blocks);
774754
aot_block_stack_destroy(&func_ctx->block_stack);
775755
wasm_runtime_free(func_ctx);
776756
return NULL;
@@ -785,8 +765,6 @@ aot_destroy_func_contexts(AOTFuncContext **func_ctxes, uint32 count)
785765
if (func_ctxes[i]) {
786766
if (func_ctxes[i]->mem_info)
787767
wasm_runtime_free(func_ctxes[i]->mem_info);
788-
if (func_ctxes[i]->exception_blocks)
789-
wasm_runtime_free(func_ctxes[i]->exception_blocks);
790768
aot_block_stack_destroy(&func_ctxes[i]->block_stack);
791769
aot_checked_addr_list_destroy(func_ctxes[i]);
792770
wasm_runtime_free(func_ctxes[i]);

0 commit comments

Comments
 (0)