Skip to content

Commit 6754b62

Browse files
authored
Optimize for multi-module support in AOT mode (#3563)
- Split the `aot_loader_resolve_function` into two functions to prevent redundant module lookups and loads - Access pre-associated module instances from `import_func_module_insts`, avoiding unnecessary instance lookups and improving performance
1 parent 867dbd8 commit 6754b62

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,17 @@ str2uint64(const char *buf, uint64 *p_res);
591591

592592
#if WASM_ENABLE_MULTI_MODULE != 0
593593
static void *
594-
aot_loader_resolve_function(const char *module_name, const char *function_name,
594+
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
595595
const AOTFuncType *expected_function_type,
596-
char *error_buf, uint32 error_buf_size)
596+
char *error_buf, uint32 error_buf_size);
597+
598+
static void *
599+
aot_loader_resolve_function_ex(const char *module_name,
600+
const char *function_name,
601+
const AOTFuncType *expected_function_type,
602+
char *error_buf, uint32 error_buf_size)
597603
{
598604
WASMModuleCommon *module_reg;
599-
void *function = NULL;
600-
AOTExport *export = NULL;
601-
AOTModule *module = NULL;
602-
AOTFuncType *target_function_type = NULL;
603605

604606
module_reg = wasm_runtime_find_module_registered(module_name);
605607
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
@@ -608,10 +610,23 @@ aot_loader_resolve_function(const char *module_name, const char *function_name,
608610
set_error_buf(error_buf, error_buf_size, "unknown import");
609611
return NULL;
610612
}
613+
return aot_loader_resolve_function((AOTModule *)module_reg, function_name,
614+
expected_function_type, error_buf,
615+
error_buf_size);
616+
}
611617

612-
module = (AOTModule *)module_reg;
613-
export = loader_find_export(module_reg, module_name, function_name,
614-
EXPORT_KIND_FUNC, error_buf, error_buf_size);
618+
static void *
619+
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
620+
const AOTFuncType *expected_function_type,
621+
char *error_buf, uint32 error_buf_size)
622+
{
623+
void *function = NULL;
624+
AOTExport *export = NULL;
625+
AOTFuncType *target_function_type = NULL;
626+
627+
export = loader_find_export((WASMModuleCommon *)module, module->name,
628+
function_name, EXPORT_KIND_FUNC, error_buf,
629+
error_buf_size);
615630
if (!export) {
616631
return NULL;
617632
}
@@ -633,7 +648,7 @@ aot_loader_resolve_function(const char *module_name, const char *function_name,
633648
if (!wasm_type_equal((WASMType *)expected_function_type,
634649
(WASMType *)target_function_type, module->types,
635650
module->type_count)) {
636-
LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
651+
LOG_DEBUG("%s.%s failed the type check", module->name, function_name);
637652
set_error_buf(error_buf, error_buf_size, "incompatible import type");
638653
return NULL;
639654
}
@@ -2260,17 +2275,24 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
22602275
&import_funcs[i].signature, &import_funcs[i].attachment,
22612276
&import_funcs[i].call_conv_raw);
22622277
if (!linked_func) {
2278+
sub_module = NULL;
22632279
if (!wasm_runtime_is_built_in_module(module_name)) {
22642280
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
22652281
(WASMModuleCommon *)module, module_name, error_buf,
22662282
error_buf_size);
22672283
if (!sub_module) {
2284+
LOG_ERROR("failed to load sub module: %s", error_buf);
22682285
return false;
22692286
}
22702287
}
2271-
linked_func = aot_loader_resolve_function(
2272-
module_name, field_name, declare_func_type, error_buf,
2273-
error_buf_size);
2288+
if (!sub_module)
2289+
linked_func = aot_loader_resolve_function_ex(
2290+
module_name, field_name, declare_func_type, error_buf,
2291+
error_buf_size);
2292+
else
2293+
linked_func = aot_loader_resolve_function(
2294+
sub_module, field_name, declare_func_type, error_buf,
2295+
error_buf_size);
22742296
}
22752297
import_funcs[i].func_ptr_linked = linked_func;
22762298
import_funcs[i].func_type = declare_func_type;

core/iwasm/aot/aot_runtime.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,16 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
16401640

16411641
#if WASM_ENABLE_MULTI_MODULE != 0
16421642
extra->sub_module_inst_list = &extra->sub_module_inst_list_head;
1643+
1644+
/* Allocate memory for import_func_module_insts*/
1645+
if (module->import_func_count > 0
1646+
&& !(extra->import_func_module_insts =
1647+
runtime_malloc((uint64)module->import_func_count
1648+
* sizeof(WASMModuleInstanceCommon *),
1649+
error_buf, error_buf_size))) {
1650+
goto fail;
1651+
}
1652+
16431653
ret = wasm_runtime_sub_module_instantiate(
16441654
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
16451655
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
@@ -1980,6 +1990,8 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19801990
#if WASM_ENABLE_MULTI_MODULE != 0
19811991
wasm_runtime_sub_module_deinstantiate(
19821992
(WASMModuleInstanceCommon *)module_inst);
1993+
if (extra->import_func_module_insts)
1994+
wasm_runtime_free(extra->import_func_module_insts);
19831995
#endif
19841996

19851997
if (module_inst->tables)
@@ -2835,10 +2847,6 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
28352847
void *attachment;
28362848
char buf[96];
28372849
bool ret = false;
2838-
#if WASM_ENABLE_MULTI_MODULE != 0
2839-
bh_list *sub_module_list_node = NULL;
2840-
const char *sub_inst_name = NULL;
2841-
#endif
28422850
bh_assert(func_idx < aot_module->import_func_count);
28432851

28442852
import_func = aot_module->import_funcs + func_idx;
@@ -2863,20 +2871,10 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
28632871
else if (!import_func->call_conv_raw) {
28642872
signature = import_func->signature;
28652873
#if WASM_ENABLE_MULTI_MODULE != 0
2866-
sub_module_list_node =
2867-
((AOTModuleInstanceExtra *)module_inst->e)->sub_module_inst_list;
2868-
sub_module_list_node = bh_list_first_elem(sub_module_list_node);
2869-
while (sub_module_list_node) {
2870-
sub_inst_name =
2871-
((AOTSubModInstNode *)sub_module_list_node)->module_name;
2872-
if (strcmp(sub_inst_name, import_func->module_name) == 0) {
2873-
exec_env = wasm_runtime_get_exec_env_singleton(
2874-
(WASMModuleInstanceCommon *)((AOTSubModInstNode *)
2875-
sub_module_list_node)
2876-
->module_inst);
2877-
break;
2878-
}
2879-
sub_module_list_node = bh_list_elem_next(sub_module_list_node);
2874+
WASMModuleInstanceCommon *sub_inst = NULL;
2875+
if ((sub_inst = ((AOTModuleInstanceExtra *)module_inst->e)
2876+
->import_func_module_insts[func_idx])) {
2877+
exec_env = wasm_runtime_get_exec_env_singleton(sub_inst);
28802878
}
28812879
if (exec_env == NULL) {
28822880
wasm_runtime_set_exception((WASMModuleInstanceCommon *)module_inst,

core/iwasm/aot/aot_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ typedef struct AOTModuleInstanceExtra {
109109
#if WASM_ENABLE_MULTI_MODULE != 0
110110
bh_list sub_module_inst_list_head;
111111
bh_list *sub_module_inst_list;
112+
WASMModuleInstanceCommon **import_func_module_insts;
112113
#endif
113114
} AOTModuleInstanceExtra;
114115

core/iwasm/common/wasm_runtime_common.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7325,6 +7325,32 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
73257325
(WASMModuleInstance *)sub_module_inst;
73267326
sub_module_inst_list_node->module_name =
73277327
sub_module_list_node->module_name;
7328+
7329+
#if WASM_ENABLE_AOT != 0
7330+
if (module_inst->module_type == Wasm_Module_AoT) {
7331+
AOTModuleInstance *aot_module_inst =
7332+
(AOTModuleInstance *)module_inst;
7333+
AOTModule *aot_module = (AOTModule *)module;
7334+
AOTModuleInstanceExtra *aot_extra =
7335+
(AOTModuleInstanceExtra *)aot_module_inst->e;
7336+
uint32 i;
7337+
AOTImportFunc *import_func;
7338+
for (i = 0; i < aot_module->import_func_count; i++) {
7339+
if (aot_extra->import_func_module_insts[i])
7340+
continue;
7341+
7342+
import_func = &aot_module->import_funcs[i];
7343+
if (strcmp(sub_module_inst_list_node->module_name,
7344+
import_func->module_name)
7345+
== 0) {
7346+
aot_extra->import_func_module_insts[i] =
7347+
(WASMModuleInstanceCommon *)
7348+
sub_module_inst_list_node->module_inst;
7349+
}
7350+
}
7351+
}
7352+
#endif
7353+
73287354
bh_list_status ret =
73297355
bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
73307356
bh_assert(BH_LIST_SUCCESS == ret);

0 commit comments

Comments
 (0)