Refine aot exception throw, remove unnecessary labels (#456)

This commit is contained in:
Wenyong Huang 2020-11-30 17:57:22 +08:00 committed by GitHub
parent 0700dc9cd4
commit 591e4ce536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 75 deletions

View File

@ -1770,13 +1770,6 @@ build_atomic_rmw:
if (last_block != func_ctx->got_exception_block) if (last_block != func_ctx->got_exception_block)
LLVMMoveBasicBlockAfter(func_ctx->got_exception_block, LLVMMoveBasicBlockAfter(func_ctx->got_exception_block,
last_block); last_block);
/* Move all other exception blocks before got_exception block */
for (i = 0; i < EXCE_NUM; i++) {
if (func_ctx->exception_blocks[i])
LLVMMoveBasicBlockBefore(func_ctx->exception_blocks[i],
func_ctx->got_exception_block);
}
} }
return true; return true;

View File

@ -6,22 +6,6 @@
#include "aot_emit_exception.h" #include "aot_emit_exception.h"
#include "../aot/aot_runtime.h" #include "../aot/aot_runtime.h"
static char *exce_block_names[] = {
"exce_unreachable", /* EXCE_UNREACHABLE */
"exce_out_of_memory", /* EXCE_OUT_OF_MEMORY */
"exce_out_of_bounds_mem_access",/* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
"exce_integer_overflow", /* EXCE_INTEGER_OVERFLOW */
"exce_divide_by_zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
"exce_invalid_convert_to_int", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
"exce_invalid_func_type_idx", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
"exce_invalid_func_idx", /* EXCE_INVALID_FUNCTION_INDEX */
"exce_undefined_element", /* EXCE_UNDEFINED_ELEMENT */
"exce_uninit_element", /* EXCE_UNINITIALIZED_ELEMENT */
"exce_call_unlinked", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
"exce_native_stack_overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
"exce_unaligned_atomic" /* EXCE_UNALIGNED_ATOMIC */
};
bool bool
aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
int32 exception_id, int32 exception_id,
@ -29,7 +13,6 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef cond_br_if, LLVMValueRef cond_br_if,
LLVMBasicBlockRef cond_br_else_block) LLVMBasicBlockRef cond_br_else_block)
{ {
LLVMBasicBlockRef exce_block;
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder); LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func; LLVMValueRef exce_id = I32_CONST((uint32)exception_id), func_const, func;
LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type; LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
@ -116,36 +99,12 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr); LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
} }
/* Create exception block if needed */ /* Add phi incoming value to got_exception block */
if (!(exce_block = func_ctx->exception_blocks[exception_id])) { LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &block_curr, 1);
if (!(func_ctx->exception_blocks[exception_id] = exce_block =
LLVMAppendBasicBlockInContext(comp_ctx->context,
func_ctx->func,
exce_block_names[exception_id]))) {
aot_set_last_error("add LLVM basic block failed.");
return false;
}
/* Move before got_exception block */
LLVMMoveBasicBlockBefore(exce_block, func_ctx->got_exception_block);
/* Add phi incoming value to got_exception block */
LLVMAddIncoming(func_ctx->exception_id_phi, &exce_id, &exce_block, 1);
/* Jump to got exception block */
LLVMPositionBuilderAtEnd(comp_ctx->builder, exce_block);
if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
aot_set_last_error("llvm build br failed.");
return false;
}
}
/* Resume builder position */
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
if (!is_cond_br) { if (!is_cond_br) {
/* not condition br, create br IR */ /* not condition br, create br IR */
if (!LLVMBuildBr(comp_ctx->builder, exce_block)) { if (!LLVMBuildBr(comp_ctx->builder, func_ctx->got_exception_block)) {
aot_set_last_error("llvm build br failed."); aot_set_last_error("llvm build br failed.");
return false; return false;
} }
@ -153,7 +112,7 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
else { else {
/* Create condition br */ /* Create condition br */
if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if, if (!LLVMBuildCondBr(comp_ctx->builder, cond_br_if,
exce_block, cond_br_else_block)) { func_ctx->got_exception_block, cond_br_else_block)) {
aot_set_last_error("llvm build cond br failed."); aot_set_last_error("llvm build cond br failed.");
return false; return false;
} }

View File

@ -99,7 +99,7 @@ check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
/* Add check exection success block */ /* Add check exection success block */
if (!(check_call_succ = LLVMAppendBasicBlockInContext(comp_ctx->context, if (!(check_call_succ = LLVMAppendBasicBlockInContext(comp_ctx->context,
func_ctx->func, func_ctx->func,
"check_exce_succ"))) { "check_call_succ"))) {
aot_set_last_error("llvm add basic block failed."); aot_set_last_error("llvm add basic block failed.");
return false; return false;
} }

View File

@ -172,20 +172,6 @@ fail:
return NULL; return NULL;
} }
static bool
create_exception_blocks(AOTFuncContext *func_ctx)
{
if (!(func_ctx->exception_blocks =
wasm_runtime_malloc(sizeof(LLVMBasicBlockRef) * EXCE_NUM))) {
aot_set_last_error("allocate memory failed.");
return false;;
}
memset(func_ctx->exception_blocks, 0,
sizeof(LLVMBasicBlockRef) * EXCE_NUM);
return true;
}
static bool static bool
create_memory_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, create_memory_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMTypeRef int8_ptr_type, uint32 func_index) LLVMTypeRef int8_ptr_type, uint32 func_index)
@ -744,10 +730,6 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
goto fail; goto fail;
} }
/* Create exception blocks */
if (!create_exception_blocks(func_ctx))
goto fail;
/* Create base addr, end addr, data size of mem, heap */ /* Create base addr, end addr, data size of mem, heap */
if (!create_memory_info(comp_ctx, func_ctx, int8_ptr_type, func_index)) if (!create_memory_info(comp_ctx, func_ctx, int8_ptr_type, func_index))
goto fail; goto fail;
@ -769,8 +751,6 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
fail: fail:
if (func_ctx->mem_info) if (func_ctx->mem_info)
wasm_runtime_free(func_ctx->mem_info); wasm_runtime_free(func_ctx->mem_info);
if (func_ctx->exception_blocks)
wasm_runtime_free(func_ctx->exception_blocks);
aot_block_stack_destroy(&func_ctx->block_stack); aot_block_stack_destroy(&func_ctx->block_stack);
wasm_runtime_free(func_ctx); wasm_runtime_free(func_ctx);
return NULL; return NULL;
@ -785,8 +765,6 @@ aot_destroy_func_contexts(AOTFuncContext **func_ctxes, uint32 count)
if (func_ctxes[i]) { if (func_ctxes[i]) {
if (func_ctxes[i]->mem_info) if (func_ctxes[i]->mem_info)
wasm_runtime_free(func_ctxes[i]->mem_info); wasm_runtime_free(func_ctxes[i]->mem_info);
if (func_ctxes[i]->exception_blocks)
wasm_runtime_free(func_ctxes[i]->exception_blocks);
aot_block_stack_destroy(&func_ctxes[i]->block_stack); aot_block_stack_destroy(&func_ctxes[i]->block_stack);
aot_checked_addr_list_destroy(func_ctxes[i]); aot_checked_addr_list_destroy(func_ctxes[i]);
wasm_runtime_free(func_ctxes[i]); wasm_runtime_free(func_ctxes[i]);