Skip to content

Commit b21f17d

Browse files
authored
Refine AOT/JIT code call wasm-c-api import process (#2982)
Allow to invoke the quick call entry wasm_runtime_quick_invoke_c_api_import to call the wasm-c-api import functions to speedup the calling process, which reduces the data copying. Use `wamrc --invoke-c-api-import` to generate the optimized AOT code, and set `jit_options->quick_invoke_c_api_import` true in wasm_engine_new when LLVM JIT is enabled.
1 parent 7c76848 commit b21f17d

File tree

20 files changed

+393
-35
lines changed

20 files changed

+393
-35
lines changed

core/iwasm/aot/aot_reloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ typedef struct {
136136
REG_SYM(aot_enlarge_memory), \
137137
REG_SYM(aot_set_exception), \
138138
REG_SYM(aot_check_app_addr_and_convert),\
139+
REG_SYM(wasm_runtime_quick_invoke_c_api_native),\
139140
{ "memset", (void*)aot_memset }, \
140141
{ "memmove", (void*)aot_memmove }, \
141142
{ "memcpy", (void*)aot_memmove }, \

core/iwasm/aot/aot_runtime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ bh_static_assert(sizeof(AOTMemoryInstance) == 104);
4747
bh_static_assert(offsetof(AOTTableInstance, elems) == 8);
4848

4949
bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
50+
bh_static_assert(offsetof(AOTModuleInstanceExtra, common.c_api_func_imports)
51+
== sizeof(uint64));
52+
53+
bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);
54+
55+
bh_static_assert(sizeof(wasm_val_t) == 16);
56+
bh_static_assert(offsetof(wasm_val_t, of) == 8);
5057

5158
static void
5259
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)

core/iwasm/common/wasm_c_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ wasm_engine_new_internal(wasm_config_t *config)
378378
wasm_engine_t *engine = NULL;
379379
/* init runtime */
380380
RuntimeInitArgs init_args = { 0 };
381+
#if WASM_ENABLE_JIT != 0
382+
LLVMJITOptions *jit_options = wasm_runtime_get_llvm_jit_options();
383+
#endif
381384

382385
#ifndef NDEBUG
383386
bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
@@ -394,6 +397,10 @@ wasm_engine_new_internal(wasm_config_t *config)
394397
init_args.enable_linux_perf = config->enable_linux_perf;
395398
init_args.segue_flags = config->segue_flags;
396399

400+
#if WASM_ENABLE_JIT != 0
401+
jit_options->quick_invoke_c_api_import = true;
402+
#endif
403+
397404
if (!wasm_runtime_full_init(&init_args)) {
398405
LOG_DEBUG("wasm_runtime_full_init failed");
399406
goto failed;

core/iwasm/common/wasm_runtime_common.c

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ static JitCompOptions jit_options = { 0 };
158158
#endif
159159

160160
#if WASM_ENABLE_JIT != 0
161-
static LLVMJITOptions llvm_jit_options = { 3, 3, 0 };
161+
/* opt_level: 3, size_level: 3, segue-flags: 0,
162+
quick_invoke_c_api_import: false */
163+
static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
162164
#endif
163165

164166
static RunningMode runtime_running_mode = Mode_Default;
@@ -638,10 +640,10 @@ wasm_runtime_get_default_running_mode(void)
638640
}
639641

640642
#if WASM_ENABLE_JIT != 0
641-
LLVMJITOptions
643+
LLVMJITOptions *
642644
wasm_runtime_get_llvm_jit_options(void)
643645
{
644-
return llvm_jit_options;
646+
return &llvm_jit_options;
645647
}
646648
#endif
647649

@@ -5675,7 +5677,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
56755677
wasm_val_t *params = params_buf, *results = results_buf;
56765678
wasm_trap_t *trap = NULL;
56775679
bool ret = false;
5678-
wasm_val_vec_t params_vec, results_vec;
5680+
wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
56795681

56805682
if (func_type->param_count > 16) {
56815683
if (!(params =
@@ -5703,12 +5705,10 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
57035705
params_vec.data = params;
57045706
params_vec.num_elems = func_type->param_count;
57055707
params_vec.size = func_type->param_count;
5706-
params_vec.size_of_elem = sizeof(wasm_val_t);
57075708

57085709
results_vec.data = results;
57095710
results_vec.num_elems = 0;
57105711
results_vec.size = func_type->result_count;
5711-
results_vec.size_of_elem = sizeof(wasm_val_t);
57125712

57135713
if (!with_env) {
57145714
wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
@@ -5744,7 +5744,6 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
57445744
wasm_runtime_set_exception(module_inst, "unsupported result type");
57455745
goto fail;
57465746
}
5747-
results_vec.num_elems = func_type->result_count;
57485747
ret = true;
57495748

57505749
fail:
@@ -5755,6 +5754,71 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
57555754
return ret;
57565755
}
57575756

5757+
bool
5758+
wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *inst_comm,
5759+
CApiFuncImport *c_api_import,
5760+
wasm_val_t *params, uint32 param_count,
5761+
wasm_val_t *results, uint32 result_count)
5762+
{
5763+
WASMModuleInstance *module_inst = (WASMModuleInstance *)inst_comm;
5764+
void *func_ptr = c_api_import->func_ptr_linked;
5765+
bool with_env_arg = c_api_import->with_env_arg, ret = true;
5766+
wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
5767+
wasm_trap_t *trap = NULL;
5768+
5769+
params_vec.data = params;
5770+
params_vec.num_elems = param_count;
5771+
params_vec.size = param_count;
5772+
5773+
results_vec.data = results;
5774+
results_vec.num_elems = 0;
5775+
results_vec.size = result_count;
5776+
5777+
if (!func_ptr) {
5778+
wasm_set_exception_with_id(module_inst, EXCE_CALL_UNLINKED_IMPORT_FUNC);
5779+
ret = false;
5780+
goto fail;
5781+
}
5782+
5783+
if (!with_env_arg) {
5784+
wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
5785+
trap = callback(&params_vec, &results_vec);
5786+
}
5787+
else {
5788+
void *wasm_c_api_env = c_api_import->env_arg;
5789+
wasm_func_callback_with_env_t callback =
5790+
(wasm_func_callback_with_env_t)func_ptr;
5791+
trap = callback(wasm_c_api_env, &params_vec, &results_vec);
5792+
}
5793+
5794+
if (trap) {
5795+
if (trap->message->data) {
5796+
/* since trap->message->data does not end with '\0' */
5797+
char trap_message[108] = { 0 };
5798+
uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
5799+
uint32 size_to_copy = (trap->message->size < max_size_to_copy)
5800+
? (uint32)trap->message->size
5801+
: max_size_to_copy;
5802+
bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
5803+
trap->message->data, size_to_copy);
5804+
wasm_set_exception(module_inst, trap_message);
5805+
}
5806+
else {
5807+
wasm_set_exception(module_inst,
5808+
"native function throw unknown exception");
5809+
}
5810+
wasm_trap_delete(trap);
5811+
ret = false;
5812+
}
5813+
5814+
fail:
5815+
#ifdef OS_ENABLE_HW_BOUND_CHECK
5816+
if (!ret)
5817+
wasm_runtime_access_exce_check_guard_page();
5818+
#endif
5819+
return ret;
5820+
}
5821+
57585822
void
57595823
wasm_runtime_show_app_heap_corrupted_prompt()
57605824
{

core/iwasm/common/wasm_runtime_common.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ typedef struct LLVMJITOptions {
443443
uint32 opt_level;
444444
uint32 size_level;
445445
uint32 segue_flags;
446+
bool quick_invoke_c_api_import;
446447
} LLVMJITOptions;
447448
#endif
448449

@@ -476,7 +477,7 @@ wasm_runtime_get_default_running_mode(void);
476477

477478
#if WASM_ENABLE_JIT != 0
478479
/* Internal API */
479-
LLVMJITOptions
480+
LLVMJITOptions *
480481
wasm_runtime_get_llvm_jit_options(void);
481482
#endif
482483

@@ -1079,6 +1080,16 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
10791080
uint32 argc, uint32 *argv, bool with_env,
10801081
void *wasm_c_api_env);
10811082

1083+
struct CApiFuncImport;
1084+
/* A quick version of wasm_runtime_invoke_c_api_native to directly invoke
1085+
wasm-c-api import function from jitted code to improve performance */
1086+
bool
1087+
wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
1088+
struct CApiFuncImport *c_api_import,
1089+
wasm_val_t *params, uint32 param_count,
1090+
wasm_val_t *results,
1091+
uint32 result_count);
1092+
10821093
void
10831094
wasm_runtime_show_app_heap_corrupted_prompt();
10841095

0 commit comments

Comments
 (0)