Refine aot call_indirect opcode translation (#492)

Re-implement aot call_indirect opcode translation: when calling non-import function, translate it by LLVM call IR to call the function in AOTed code, so as to avoid calling runtime aot_call_indirect API which is much slower. For import function, keep calling aot_call_indirect API due to the possible pointer/string argument conversion.

And add prompt info while app heap is corrupted, change emit_leb to emit_uint32 inter fast-interp to refine footprint.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-01-05 04:05:30 -06:00
committed by GitHub
parent f2a63d8623
commit 788cbf2a19
15 changed files with 529 additions and 191 deletions

View File

@ -517,6 +517,19 @@ wasm_type_equal(const WASMType *type1, const WASMType *type2)
? true : false;
}
inline static uint32
wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
uint32 cur_type_idx)
{
uint32 i;
for (i = 0; i < cur_type_idx; i++) {
if (wasm_type_equal(types[cur_type_idx], types[i]))
return i;
}
return cur_type_idx;
}
static inline uint32
block_type_get_param_types(BlockType *block_type,
uint8 **p_param_types)

View File

@ -380,48 +380,11 @@ popcount64(uint64 u)
return ret;
}
static uint64
read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
{
uint64 result = 0;
uint32 shift = 0;
uint32 bcnt = 0;
uint64 byte;
while (true) {
byte = buf[*p_offset];
*p_offset += 1;
result |= ((byte & 0x7f) << shift);
shift += 7;
if ((byte & 0x80) == 0) {
break;
}
bcnt += 1;
}
if (sign && (shift < maxbits) && (byte & 0x40)) {
/* Sign extend */
result |= - ((uint64)1 << shift);
}
return result;
}
#define read_leb_uint32(p, p_end, res) do { \
uint8 _val = *p; \
if (!(_val & 0x80)) { \
res = _val; \
p++; \
break; \
} \
uint32 _off = 0; \
res = (uint32)read_leb(p, &_off, 32, false); \
p += _off; \
} while (0)
#define read_uint32(p) (p += sizeof(uint32), *(uint32 *)(p - sizeof(uint32)))
#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
uint32 param_count = cur_func->param_count; \
read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
local_idx = read_uint32(frame_ip); \
bh_assert(local_idx < param_count + cur_func->local_count); \
local_offset = cur_func->local_offsets[local_idx]; \
if (local_idx < param_count) \

View File

@ -791,6 +791,12 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
return false;
}
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
declare_type_index = wasm_get_smallest_type_idx(
parent_module->types, parent_module->type_count,
declare_type_index);
#endif
declare_func_type = parent_module->types[declare_type_index];
if (wasm_runtime_is_host_module(sub_module_name)) {
@ -1724,6 +1730,11 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
return false;
}
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
type_index = wasm_get_smallest_type_idx(
module->types, module->type_count, type_index);
#endif
read_leb_uint32(p_code, buf_code_end, code_size);
if (code_size == 0
|| p_code + code_size > buf_code_end) {
@ -4312,10 +4323,6 @@ fail:
LOG_OP("%d\t", value); \
} while (0)
#define emit_leb() do { \
wasm_loader_emit_leb(loader_ctx, p_org, p); \
} while (0)
static bool
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
{
@ -4402,21 +4409,6 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
ctx->code_compiled_size -= size;
}
static void
wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
{
if (ctx->p_code_compiled) {
bh_memcpy_s(ctx->p_code_compiled,
ctx->p_code_compiled_end - ctx->p_code_compiled,
start, end - start);
ctx->p_code_compiled += (end - start);
}
else {
ctx->code_compiled_size += (end - start);
}
}
static bool
preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
uint32 local_index, uint32 local_type, bool *preserved,
@ -6497,8 +6489,7 @@ handle_op_block_and_loop:
}
}
else { /* local index larger than 255, reserve leb */
p_org ++;
emit_leb();
emit_uint32(loader_ctx, local_idx);
POP_OFFSET_TYPE(local_type);
}
#else
@ -6554,11 +6545,10 @@ handle_op_block_and_loop:
}
}
else { /* local index larger than 255, reserve leb */
p_org ++;
emit_leb();
emit_uint32(loader_ctx, local_idx);
}
emit_operand(loader_ctx, *(loader_ctx->frame_offset -
wasm_value_type_cell_num(local_type)));
wasm_value_type_cell_num(local_type)));
#else
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (local_offset < 0x80) {

View File

@ -3062,10 +3062,6 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
LOG_OP("%d\t", value); \
} while (0)
#define emit_leb() do { \
wasm_loader_emit_leb(loader_ctx, p_org, p); \
} while (0)
static bool
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
{
@ -3152,21 +3148,6 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
ctx->code_compiled_size -= size;
}
static void
wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
{
if (ctx->p_code_compiled) {
bh_memcpy_s(ctx->p_code_compiled,
ctx->p_code_compiled_end - ctx->p_code_compiled,
start, end - start);
ctx->p_code_compiled += (end - start);
}
else {
ctx->code_compiled_size += (end - start);
}
}
static bool
preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
uint32 local_index, uint32 local_type, bool *preserved,
@ -4970,8 +4951,7 @@ handle_op_block_and_loop:
}
}
else { /* local index larger than 255, reserve leb */
p_org ++;
emit_leb();
emit_uint32(loader_ctx, local_idx);
POP_OFFSET_TYPE(local_type);
}
#else
@ -5027,11 +5007,10 @@ handle_op_block_and_loop:
}
}
else { /* local index larger than 255, reserve leb */
p_org ++;
emit_leb();
emit_uint32(loader_ctx, local_idx);
}
emit_operand(loader_ctx, *(loader_ctx->frame_offset -
wasm_value_type_cell_num(local_type)));
wasm_value_type_cell_num(local_type)));
#else
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (local_offset < 0x80) {

View File

@ -1688,7 +1688,17 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
}
if (!addr) {
wasm_set_exception(module_inst, "out of memory");
if (memory->heap_handle
&& mem_allocator_is_heap_corrupted(memory->heap_handle)) {
LOG_ERROR("Error: app heap is corrupted, if the wasm file "
"is compiled by wasi-sdk-12.0 or larger version, "
"please add -Wl,--export=malloc -Wl,--export=free "
" to export malloc and free functions.");
wasm_set_exception(module_inst, "app heap corrupted");
}
else {
wasm_set_exception(module_inst, "out of memory");
}
return 0;
}
if (p_native_addr)