From 08eeeb748c47bf9594bd6346657cbc4c6f129113 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 24 Aug 2022 14:05:51 +0800 Subject: [PATCH] Add more fast jit fixed virtual regs (#1409) Add fast jit fixed virtual register import_func_ptrs_reg and func_type_indexes_reg for the preparation of further optimizations. --- core/iwasm/fast-jit/jit_frontend.c | 34 ++++++++++++++++++ core/iwasm/fast-jit/jit_frontend.h | 6 ++++ core/iwasm/fast-jit/jit_ir.h | 8 +++++ core/iwasm/interpreter/wasm_runtime.c | 50 +++++++++++++++++++++++++++ core/iwasm/interpreter/wasm_runtime.h | 1 + 5 files changed, 99 insertions(+) diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 117df9381..124a5f553 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -70,6 +70,21 @@ get_module_reg(JitFrame *frame) return frame->module_reg; } +JitReg +get_import_func_ptrs_reg(JitFrame *frame) +{ + JitCompContext *cc = frame->cc; + JitReg module_inst_reg = get_module_inst_reg(frame); + + if (!frame->import_func_ptrs_reg) { + frame->import_func_ptrs_reg = cc->import_func_ptrs_reg; + GEN_INSN( + LDPTR, frame->import_func_ptrs_reg, module_inst_reg, + NEW_CONST(I32, offsetof(WASMModuleInstance, import_func_ptrs))); + } + return frame->import_func_ptrs_reg; +} + JitReg get_fast_jit_func_ptrs_reg(JitFrame *frame) { @@ -85,6 +100,21 @@ get_fast_jit_func_ptrs_reg(JitFrame *frame) return frame->fast_jit_func_ptrs_reg; } +JitReg +get_func_type_indexes_reg(JitFrame *frame) +{ + JitCompContext *cc = frame->cc; + JitReg module_inst_reg = get_module_inst_reg(frame); + + if (!frame->func_type_indexes_reg) { + frame->func_type_indexes_reg = cc->func_type_indexes_reg; + GEN_INSN( + LDPTR, frame->func_type_indexes_reg, module_inst_reg, + NEW_CONST(I32, offsetof(WASMModuleInstance, func_type_indexes))); + } + return frame->func_type_indexes_reg; +} + JitReg get_global_data_reg(JitFrame *frame) { @@ -376,7 +406,9 @@ clear_fixed_virtual_regs(JitFrame *frame) frame->module_inst_reg = 0; frame->module_reg = 0; + frame->import_func_ptrs_reg = 0; frame->fast_jit_func_ptrs_reg = 0; + frame->func_type_indexes_reg = 0; frame->global_data_reg = 0; frame->aux_stack_bound_reg = 0; frame->aux_stack_bottom_reg = 0; @@ -572,7 +604,9 @@ create_fixed_virtual_regs(JitCompContext *cc) cc->module_inst_reg = jit_cc_new_reg_ptr(cc); cc->module_reg = jit_cc_new_reg_ptr(cc); + cc->import_func_ptrs_reg = jit_cc_new_reg_ptr(cc); cc->fast_jit_func_ptrs_reg = jit_cc_new_reg_ptr(cc); + cc->func_type_indexes_reg = jit_cc_new_reg_ptr(cc); cc->global_data_reg = jit_cc_new_reg_ptr(cc); cc->aux_stack_bound_reg = jit_cc_new_reg_I32(cc); cc->aux_stack_bottom_reg = jit_cc_new_reg_I32(cc); diff --git a/core/iwasm/fast-jit/jit_frontend.h b/core/iwasm/fast-jit/jit_frontend.h index adb7ef4e4..a31fbc5ae 100644 --- a/core/iwasm/fast-jit/jit_frontend.h +++ b/core/iwasm/fast-jit/jit_frontend.h @@ -187,9 +187,15 @@ get_module_inst_reg(JitFrame *frame); JitReg get_module_reg(JitFrame *frame); +JitReg +get_import_func_ptrs_reg(JitFrame *frame); + JitReg get_fast_jit_func_ptrs_reg(JitFrame *frame); +JitReg +get_func_type_indexes_reg(JitFrame *frame); + JitReg get_global_data_reg(JitFrame *frame); diff --git a/core/iwasm/fast-jit/jit_ir.h b/core/iwasm/fast-jit/jit_ir.h index 7dc453fed..b00694fc0 100644 --- a/core/iwasm/fast-jit/jit_ir.h +++ b/core/iwasm/fast-jit/jit_ir.h @@ -909,8 +909,12 @@ typedef struct JitFrame { JitReg module_inst_reg; /* WASM module */ JitReg module_reg; + /* module_inst->import_func_ptrs */ + JitReg import_func_ptrs_reg; /* module_inst->fast_jit_func_ptrs */ JitReg fast_jit_func_ptrs_reg; + /* module_inst->func_type_indexes */ + JitReg func_type_indexes_reg; /* Base address of global data */ JitReg global_data_reg; /* Boundary of auxiliary stack */ @@ -1027,8 +1031,12 @@ typedef struct JitCompContext { JitReg module_inst_reg; /* WASM module */ JitReg module_reg; + /* module_inst->import_func_ptrs */ + JitReg import_func_ptrs_reg; /* module_inst->fast_jit_func_ptrs */ JitReg fast_jit_func_ptrs_reg; + /* module_inst->func_type_indexes */ + JitReg func_type_indexes_reg; /* Base address of global data */ JitReg global_data_reg; /* Boundary of auxiliary stack */ diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 7f3019825..065ef1139 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1262,6 +1262,47 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf, return true; } +#if WASM_ENABLE_FAST_JIT != 0 +static uint32 +get_smallest_type_idx(WASMModule *module, WASMType *func_type) +{ + uint32 i; + + for (i = 0; i < module->type_count; i++) { + if (func_type == module->types[i]) + return i; + } + + bh_assert(0); + return -1; +} + +static bool +init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf, + uint32 error_buf_size) +{ + uint32 i; + uint64 total_size = (uint64)sizeof(uint32) * module_inst->function_count; + + /* Allocate memory */ + if (!(module_inst->func_type_indexes = + runtime_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < module_inst->function_count; i++) { + WASMFunctionInstance *func_inst = module_inst->functions + i; + WASMType *func_type = func_inst->is_import_func + ? func_inst->u.func_import->func_type + : func_inst->u.func->func_type; + module_inst->func_type_indexes[i] = + get_smallest_type_idx(module_inst->module, func_type); + } + + return true; +} +#endif + /** * Instantiate module */ @@ -1384,6 +1425,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, && !(module_inst->export_globals = export_globals_instantiate( module, module_inst, module_inst->export_glob_count, error_buf, error_buf_size))) +#endif +#if WASM_ENABLE_FAST_JIT != 0 + || (module_inst->function_count > 0 + && !init_func_type_indexes(module_inst, error_buf, error_buf_size)) #endif ) { goto fail; @@ -1712,6 +1757,11 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) if (!module_inst) return; +#if WASM_ENABLE_FAST_JIT != 0 + if (module_inst->func_type_indexes) + wasm_runtime_free(module_inst->func_type_indexes); +#endif + #if WASM_ENABLE_MULTI_MODULE != 0 sub_module_deinstantiate(module_inst); #endif diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 581b80ee3..4cdbf5bad 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -191,6 +191,7 @@ struct WASMModuleInstance { #if WASM_ENABLE_FAST_JIT != 0 /* point to JITed functions */ void **fast_jit_func_ptrs; + uint32 *func_type_indexes; #endif WASMMemoryInstance **memories;