Implement shared heap for AOT (#3815)

This commit is contained in:
Wenyong Huang
2024-09-29 12:50:59 +08:00
committed by GitHub
parent c4aa1deda5
commit 9ba36e284c
29 changed files with 684 additions and 81 deletions

View File

@ -48,6 +48,7 @@ if (NOT CMAKE_BUILD_TYPE)
endif ()
set (WAMR_BUILD_INTERP 1)
set (WAMR_BUILD_FAST_INTERP 1)
set (WAMR_BUILD_AOT 1)
set (WAMR_BUILD_JIT 0)
set (WAMR_BUILD_LIBC_BUILTIN 1)
@ -72,7 +73,11 @@ endif ()
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
if (MSVC)
target_compile_definitions(vmlib PRIVATE WASM_API_EXTERN=)
endif()
target_link_libraries(vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
################ application related ################
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
@ -90,3 +95,33 @@ else ()
endif ()
add_subdirectory(wasm-apps)
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 ${WAMR_ROOT_DIR}/wamr-compiler")
else()
message (STATUS "WAMR_COMPILER is ${WAMR_COMPILER}")
endif()
add_custom_target(
wasm_to_aot
ALL
DEPENDS wasm-apps/test1.wasm wasm-apps/test2.wasm ${WAMR_COMPILER}
COMMAND ${WAMR_COMPILER} --enable-shared-heap -o wasm-apps/test1.aot wasm-apps/test1.wasm
COMMAND ${WAMR_COMPILER} --enable-shared-heap -o wasm-apps/test2.aot wasm-apps/test2.wasm
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endif()

View File

@ -19,15 +19,15 @@ thread1_callback(void *arg)
wasm_module_inst_t module_inst = targ->module_inst;
bh_queue *queue = targ->queue;
wasm_exec_env_t exec_env;
wasm_function_inst_t my_shared_malloc_func;
wasm_function_inst_t my_shared_free_func;
wasm_function_inst_t my_shared_heap_malloc_func;
wasm_function_inst_t my_shared_heap_free_func;
uint32 i, argv[2];
/* lookup wasm functions */
if (!(my_shared_malloc_func =
wasm_runtime_lookup_function(module_inst, "my_shared_malloc"))
|| !(my_shared_free_func =
wasm_runtime_lookup_function(module_inst, "my_shared_free"))) {
if (!(my_shared_heap_malloc_func = wasm_runtime_lookup_function(
module_inst, "my_shared_heap_malloc"))
|| !(my_shared_heap_free_func = wasm_runtime_lookup_function(
module_inst, "my_shared_heap_free"))) {
printf("Failed to lookup function.\n");
}
@ -62,17 +62,17 @@ thread1_callback(void *arg)
}
}
/* allocate memory by calling my_shared_malloc function and send it
/* allocate memory by calling my_shared_heap_malloc function and send it
to wasm app2 */
for (i = 5; i < 10; i++) {
uint8 *buf;
argv[0] = 1024 * (i + 1);
argv[1] = i + 1;
wasm_runtime_call_wasm(exec_env, my_shared_malloc_func, 2, argv);
wasm_runtime_call_wasm(exec_env, my_shared_heap_malloc_func, 2, argv);
if (wasm_runtime_get_exception(module_inst)) {
printf("Failed to call 'my_shared_malloc` function: %s\n",
printf("Failed to call 'my_shared_heap_malloc' function: %s\n",
wasm_runtime_get_exception(module_inst));
break;
}
@ -118,6 +118,10 @@ queue_callback(void *message, void *arg)
/* call wasm function */
argv[0] = wasm_runtime_addr_native_to_app(module_inst, buf);
wasm_runtime_call_wasm(exec_env, print_buf_func, 1, argv);
if (wasm_runtime_get_exception(module_inst)) {
printf("Failed to call 'print_buf' function: %s\n",
wasm_runtime_get_exception(module_inst));
}
}
static void *
@ -159,8 +163,17 @@ main(int argc, char **argv)
RuntimeInitArgs init_args;
SharedHeapInitArgs heap_init_args;
char error_buf[128] = { 0 };
bool aot_mode = false;
int ret = -1;
if (argc > 1 && !strcmp(argv[1], "--aot"))
aot_mode = true;
if (!aot_mode)
printf("Test shared heap in interpreter mode\n\n");
else
printf("Test shared heap in AOT mode\n\n");
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
@ -180,7 +193,10 @@ main(int argc, char **argv)
}
/* read wasm file */
wasm_file1 = "./wasm-apps/test1.wasm";
if (!aot_mode)
wasm_file1 = "./wasm-apps/test1.wasm";
else
wasm_file1 = "./wasm-apps/test1.aot";
if (!(wasm_file1_buf =
bh_read_file_to_buffer(wasm_file1, &wasm_file1_size))) {
printf("Open wasm file %s failed.\n", wasm_file1);
@ -204,7 +220,10 @@ main(int argc, char **argv)
}
/* read wasm file */
wasm_file2 = "./wasm-apps/test2.wasm";
if (!aot_mode)
wasm_file2 = "./wasm-apps/test2.wasm";
else
wasm_file2 = "./wasm-apps/test2.aot";
if (!(wasm_file2_buf =
bh_read_file_to_buffer(wasm_file2, &wasm_file2_size))) {
printf("Open wasm file %s failed.\n", wasm_file1);
@ -273,7 +292,7 @@ main(int argc, char **argv)
/* wait until all messages are post to wasm app2 and wasm app2
handles all of them, then exit the queue message loop */
usleep(2000);
usleep(10000);
bh_queue_exit_loop_run(queue);
os_thread_join(tid1, NULL);

View File

@ -26,12 +26,12 @@ set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (DEFINED_SYMBOLS "${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
set (CMAKE_EXE_LINKER_FLAGS
"-Wl,--initial-memory=65536, \
"-O0 -Wl,--initial-memory=65536, \
-Wl,--no-entry,--strip-all, \
-Wl,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors \
-Wl,--export=my_shared_malloc \
-Wl,--export=my_shared_free \
-Wl,--export=my_shared_heap_malloc \
-Wl,--export=my_shared_heap_free \
-Wl,--export=print_buf \
-Wl,--allow-undefined"
)

View File

@ -5,26 +5,56 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern void *
shared_malloc(uint32_t size);
shared_heap_malloc(uint32_t size);
extern void
shared_free(void *ptr);
shared_heap_free(void *ptr);
void *
my_shared_malloc(uint32_t size, uint32_t index)
my_shared_heap_malloc(uint32_t size, uint32_t index)
{
char *buf = shared_malloc(size);
char *buf1 = NULL, *buf2 = NULL, *buf;
if (buf)
snprintf(buf, 1024, "Hello, this is buf %u allocated from shared heap",
index);
buf1 = shared_heap_malloc(128);
if (!buf1)
return NULL;
buf1[0] = 'H';
buf1[1] = 'e';
buf1[2] = 'l';
buf1[3] = 'l';
buf1[4] = 'o';
buf1[5] = ',';
buf1[6] = ' ';
buf2 = shared_heap_malloc(128);
if (!buf2) {
shared_heap_free(buf1);
return NULL;
}
snprintf(buf2, 128, "this is buf %u allocated from shared heap", index);
buf = shared_heap_malloc(size);
if (!buf) {
shared_heap_free(buf1);
shared_heap_free(buf2);
return NULL;
}
memset(buf, 0, size);
memcpy(buf, buf1, strlen(buf1));
memcpy(buf + strlen(buf1), buf2, strlen(buf2));
shared_heap_free(buf1);
shared_heap_free(buf2);
return buf;
}
void
my_shared_free(void *ptr)
my_shared_heap_free(void *ptr)
{
shared_free(ptr);
shared_heap_free(ptr);
}

View File

@ -8,11 +8,11 @@
#include <stdio.h>
extern void
shared_free(void *ptr);
shared_heap_free(void *ptr);
void
print_buf(char *buf)
{
printf("wasm app2's wasm func received buf: %s\n\n", buf);
shared_free(buf);
shared_heap_free(buf);
}