Skip to content

Commit 7c76848

Browse files
authored
Register quick call entries to speedup the aot/jit func call process (#2978)
In some scenarios there may be lots of callings to AOT/JIT functions from the host embedder, which expects good performance for the calling process, while in the current implementation, runtime calls the wasm_runtime_invoke_native to prepare the array of registers and stacks for the invokeNative assemble code, and the latter then puts the elements in the array to physical registers and native stacks and calls the AOT/JIT function, there may be many data copying and handlings which impact the performance. This PR registers some quick AOT/JIT entries for some simple wasm signatures, and let runtime call the entry to directly invoke the AOT/JIT function instead of calling wasm_runtime_invoke_native, which speedups the calling process. We may extend the mechanism next to allow the developer to register his quick AOT/JIT entries to speedup the calling process of invoking the AOT/JIT functions for some specific signatures.
1 parent 6fa6d6d commit 7c76848

File tree

10 files changed

+1044
-143
lines changed

10 files changed

+1044
-143
lines changed

build-scripts/config_common.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,18 @@ if (WAMR_CONFIGUABLE_BOUNDS_CHECKS EQUAL 1)
449449
endif ()
450450
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
451451
add_definitions (-DWASM_ENABLE_LINUX_PERF=1)
452-
message (" Enable linux perf support")
452+
message (" Linux perf support enabled")
453+
endif ()
454+
if (NOT DEFINED WAMR_BUILD_QUICK_AOT_ENTRY)
455+
# Enable quick aot/jit entries by default
456+
set (WAMR_BUILD_QUICK_AOT_ENTRY 1)
457+
endif ()
458+
if (WAMR_BUILD_QUICK_AOT_ENTRY EQUAL 1)
459+
add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=1)
460+
message (" Quick AOT/JIT entries enabled")
461+
else ()
462+
add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=0)
463+
message (" Quick AOT/JIT entries disabled")
453464
endif ()
454465

455466
if (APPLE)

core/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,11 @@
495495
#define WASM_ENABLE_LINUX_PERF 0
496496
#endif
497497

498+
/* Support registering quick AOT/JIT function entries of some func types
499+
to speedup the calling process of invoking the AOT/JIT functions of
500+
these types from the host embedder */
501+
#ifndef WASM_ENABLE_QUICK_AOT_ENTRY
502+
#define WASM_ENABLE_QUICK_AOT_ENTRY 1
503+
#endif
504+
498505
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_loader.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,11 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
12021202

12031203
func_types[i]->param_cell_num = (uint16)param_cell_num;
12041204
func_types[i]->ret_cell_num = (uint16)ret_cell_num;
1205+
1206+
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
1207+
func_types[i]->quick_aot_entry =
1208+
wasm_native_lookup_quick_aot_entry(func_types[i]);
1209+
#endif
12051210
}
12061211

12071212
*p_buf = buf;

core/iwasm/aot/aot_runtime.c

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,9 +1385,6 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13851385
WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
13861386
uint32 page_size = os_getpagesize();
13871387
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
1388-
uint16 param_count = func_type->param_count;
1389-
uint16 result_count = func_type->result_count;
1390-
const uint8 *types = func_type->types;
13911388
#ifdef BH_PLATFORM_WINDOWS
13921389
int result;
13931390
bool has_exception;
@@ -1426,28 +1423,22 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
14261423
wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
14271424

14281425
if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
1429-
/* Quick call with func_ptr if the function signature is simple */
1430-
if (!signature && param_count == 1 && types[0] == VALUE_TYPE_I32) {
1431-
if (result_count == 0) {
1432-
void (*NativeFunc)(WASMExecEnv *, uint32) =
1433-
(void (*)(WASMExecEnv *, uint32))func_ptr;
1434-
NativeFunc(exec_env, argv[0]);
1435-
ret = aot_copy_exception(module_inst, NULL) ? false : true;
1436-
}
1437-
else if (result_count == 1
1438-
&& types[param_count] == VALUE_TYPE_I32) {
1439-
uint32 (*NativeFunc)(WASMExecEnv *, uint32) =
1440-
(uint32(*)(WASMExecEnv *, uint32))func_ptr;
1441-
argv_ret[0] = NativeFunc(exec_env, argv[0]);
1442-
ret = aot_copy_exception(module_inst, NULL) ? false : true;
1443-
}
1444-
else {
1445-
ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type,
1446-
signature, attachment, argv,
1447-
argc, argv_ret);
1448-
}
1426+
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
1427+
/* Quick call if the quick aot entry is registered */
1428+
if (!signature && func_type->quick_aot_entry) {
1429+
void (*invoke_native)(
1430+
void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv,
1431+
uint32 *argv_ret) = func_type->quick_aot_entry;
1432+
invoke_native(func_ptr,
1433+
func_type->result_count > 0
1434+
? func_type->types[func_type->param_count]
1435+
: VALUE_TYPE_VOID,
1436+
exec_env, argv, argv_ret);
1437+
ret = !aot_copy_exception(module_inst, NULL);
14491438
}
1450-
else {
1439+
else
1440+
#endif
1441+
{
14511442
ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type,
14521443
signature, attachment, argv, argc,
14531444
argv_ret);

0 commit comments

Comments
 (0)