Support muti-module for AOT mode (#2482)

Support muti-module for AOT mode, currently only implement the
multi-module's function import feature for AOT, the memory/table/
global import are not implemented yet.

And update wamr-test-suites scripts, multi-module sample and some
CIs accordingly.
This commit is contained in:
dongsheng28849455
2023-09-28 08:56:11 +08:00
committed by GitHub
parent fff0e2ad1c
commit 79b27c1934
20 changed files with 1018 additions and 343 deletions

View File

@ -43,8 +43,12 @@ if (NOT CMAKE_BUILD_TYPE)
endif ()
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_JIT 0)
if (NOT DEFINED WAMR_BUILD_AOT)
set(WAMR_BUILD_AOT 0)
endif ()
if (NOT DEFINED WAMR_BUILD_JIT)
set(WAMR_BUILD_JIT 0)
endif ()
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_LIBC_WASI 1)
set(WAMR_BUILD_MULTI_MODULE 1)
@ -144,6 +148,43 @@ ExternalProject_Add(WASM_MODULE
./mE.wasm ${CMAKE_BINARY_DIR}
)
################ WASM MODULES TO AOT
if (WAMR_BUILD_AOT EQUAL 1)
set(WAMR_COMPILER_DIR ${CMAKE_CURRENT_LIST_DIR}/../../wamr-compiler/build)
message(CHECK_START "Detecting WAMR_COMPILER at ${WAMR_COMPILER_DIR}")
find_file(WAMR_COMPILER
wamrc
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../wamr-compiler/build"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
if(WAMR_COMPILER)
message(CHECK_PASS "found")
else()
message(CHECK_FAIL "not found")
endif()
if((NOT EXISTS ${WAMR_COMPILER}) )
message(FATAL_ERROR "Please build wamrc under the path=${WAMR_ROOT_DIR}/wamr-compiler/ ")
else()
message(STATUS "WAMR_COMPILER is ${WAMR_COMPILER}")
endif()
add_custom_target(
wasm_to_aot
ALL
DEPENDS
WASM_MODULE ${WAMR_COMPILER}
COMMAND
${WAMR_COMPILER} -o mA.aot ./mA.wasm
COMMAND
${WAMR_COMPILER} -o mB.aot ./mB.wasm
COMMAND
${WAMR_COMPILER} -o mC.aot ./mC.wasm
WORKING_DIRECTORY
${CMAKE_BINARY_DIR}
)
endif()
################ NATIVE
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)

View File

@ -0,0 +1,20 @@
# WAMR MULTI-MODUEL SAMPLE
**WAMR supports *multi-module* in both *interpreter* mode and *aot* mode.**
Multi-modules will determine the running mode based on the type of the main module.
``` shell
$ mkdir build
$ cd build
$ cmake ..
$ make
$ # It will build multi-module runtime and
$ # wasm file under the ./build .
$ # If you have built wamrc,
$ # aot file will also genrate.
$ ./multi-module mC.wasm
$ ...
$ ./multi-module mC.aot
$ ...

View File

@ -1,59 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bh_read_file.h"
#include "platform_common.h"
#include "wasm_export.h"
static char *
build_module_path(const char *module_name)
#if WASM_ENABLE_MULTI_MODULE != 0
static char *module_search_path = ".";
static bool
module_reader_callback(package_type_t module_type, const char *module_name,
uint8 **p_buffer, uint32 *p_size)
{
const char *module_search_path = ".";
const char *format = "%s/%s.wasm";
char *file_format;
#if WASM_ENABLE_INTERP != 0
if (module_type == Wasm_Module_Bytecode)
file_format = ".wasm";
#endif
#if WASM_ENABLE_AOT != 0
if (module_type == Wasm_Module_AoT)
file_format = ".aot";
#endif
const char *format = "%s/%s%s";
int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
+ strlen(".wasm") + 1;
+ strlen(file_format) + 1;
char *wasm_file_name = BH_MALLOC(sz);
if (!wasm_file_name) {
return NULL;
}
snprintf(wasm_file_name, sz, format, module_search_path, module_name);
return wasm_file_name;
}
static bool
module_reader_cb(const char *module_name, uint8 **p_buffer, uint32 *p_size)
{
char *wasm_file_path = build_module_path(module_name);
if (!wasm_file_path) {
return false;
}
snprintf(wasm_file_name, sz, format, module_search_path, module_name,
file_format);
*p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
printf("- bh_read_file_to_buffer %s\n", wasm_file_path);
*p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_path, p_size);
BH_FREE(wasm_file_path);
wasm_runtime_free(wasm_file_name);
return *p_buffer != NULL;
}
static void
module_destroyer_cb(uint8 *buffer, uint32 size)
moudle_destroyer(uint8 *buffer, uint32 size)
{
printf("- release the read file buffer\n");
if (!buffer) {
return;
}
BH_FREE(buffer);
wasm_runtime_free(buffer);
buffer = NULL;
}
#endif /* WASM_ENABLE_MULTI_MODULE */
/* 10M */
static char sandbox_memory_space[10 * 1024 * 1024] = { 0 };
int
main()
main(int argc, char *argv[])
{
bool ret = false;
if (argc != 2) {
return -1;
}
char *wasm_file = argv[1];
/* 16K */
const uint32 stack_size = 16 * 1024;
const uint32 heap_size = 16 * 1024;
@ -84,16 +88,16 @@ main()
#if WASM_ENABLE_MULTI_MODULE != 0
printf("- wasm_runtime_set_module_reader\n");
/* set module reader and destroyer */
wasm_runtime_set_module_reader(module_reader_cb, module_destroyer_cb);
wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
#endif
/* load WASM byte buffer from WASM bin file */
if (!module_reader_cb("mC", &file_buf, &file_buf_size)) {
if (!(file_buf =
(uint8 *)bh_read_file_to_buffer(wasm_file, &file_buf_size)))
goto RELEASE_RUNTIME;
}
/* load mC and let WAMR load mA and mB */
printf("- wasm_runtime_load\n");
if (!(module = wasm_runtime_load(file_buf, file_buf_size, error_buf,
sizeof(error_buf)))) {
printf("%s\n", error_buf);
@ -158,7 +162,7 @@ UNLOAD_MODULE:
printf("- wasm_runtime_unload\n");
wasm_runtime_unload(module);
RELEASE_BINARY:
module_destroyer_cb(file_buf, file_buf_size);
moudle_destroyer(file_buf, file_buf_size);
RELEASE_RUNTIME:
printf("- wasm_runtime_destroy\n");
wasm_runtime_destroy();