Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[instantiation linking] Create and import WASMGlobalInstance #3914

Draft
wants to merge 37 commits into
base: dev/instantiate_linking
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d1139cc
Allow .aot files in raw demo
lum1n0us Oct 27, 2024
9189eae
Allow to emit and load import_memory info
lum1n0us Oct 28, 2024
14de319
create/destroy memory api for aot
lum1n0us Oct 28, 2024
505dbef
Refactor memory instantiation logic
lum1n0us Oct 30, 2024
e4fd6ee
Refactor AOT memory handling and improve memory initialization logic
lum1n0us Oct 31, 2024
454d576
fix memory instantiation issues
lum1n0us Oct 31, 2024
929e4a3
Refactor aot_instantiate function parameters for improved clarity
lum1n0us Nov 1, 2024
ac405d3
Add wasm_runtime_get_extern_instance function and refactor memory ins…
lum1n0us Nov 1, 2024
365cee9
Implement import inheritance and disinheritance functions for WASI th…
lum1n0us Nov 1, 2024
878287d
Fix memory deinstantiation loop to correctly reference import_memory_…
lum1n0us Nov 1, 2024
27a5959
Refactor wasm_runtime_instantiate()
lum1n0us Nov 1, 2024
b8952b7
Remove unused disinherit imports functions
lum1n0us Nov 3, 2024
c128a4b
Implement wasm_runtime_instantiate_with_inheritance function and refa…
lum1n0us Nov 3, 2024
b71bee8
Refactor memory loading functions for clarity and consistency
lum1n0us Nov 4, 2024
5774e9d
Implement import table creation and table creation in AOT compilation
lum1n0us Nov 4, 2024
305b490
Enhance import handling during AOT compilation and streamline related…
lum1n0us Nov 4, 2024
e986e74
Add import table demo and its AOT compilation
lum1n0us Nov 6, 2024
b02ef37
Add table creation and destruction functions for AOT and interpreter
lum1n0us Nov 6, 2024
887a143
[draft] include refactor wasm_table_inst_t
lum1n0us Nov 6, 2024
a09dd90
Implement table element location functions for AOT and interpreter
lum1n0us Nov 8, 2024
eaf77e4
Fix table instantiation logic to account for imported tables
lum1n0us Nov 8, 2024
1be95ec
Fix table element retrieval to correctly handle pointer dereferencing…
lum1n0us Nov 8, 2024
89327cc
Refactor table element retrieval to avoid redundant calls in tables_i…
lum1n0us Nov 8, 2024
06f2f6f
Refactor table element base retrieval to improve clarity and reduce r…
lum1n0us Nov 8, 2024
6c85905
Update placeholder comments and adjust string reading for import tabl…
lum1n0us Nov 8, 2024
7b58edf
Refactor table element access to use wasm_locate_table_elems for impr…
lum1n0us Nov 10, 2024
d74a8fc
Remove unsupported table element setting functions and improve table …
lum1n0us Nov 10, 2024
ad3541c
Enhance table element retrieval for imported tables in JIT frontend
lum1n0us Nov 10, 2024
5e9abda
Refactor table element retrieval to use temporary variable for improv…
lum1n0us Nov 10, 2024
e3f3299
Fix table element retrieval in wasm_table_set to use pointer arithmet…
lum1n0us Nov 10, 2024
39c2cf1
Fix table element retrieval to use correct table instance in wasm_ins…
lum1n0us Nov 10, 2024
a9e7954
Enhance table instantiation logic for multi-module support and improv…
lum1n0us Nov 11, 2024
79ce3f6
Add multi-module support and related feature flag handling
lum1n0us Nov 11, 2024
28d71b8
[rebase confliction] Refactor: streamline global instance handling an…
lum1n0us Nov 17, 2024
9c87b5e
Refactor: improve global instance handling
lum1n0us Nov 18, 2024
edf7f05
llvm-jit can read global values from local inst and import inst
lum1n0us Nov 19, 2024
92a6310
Refactor: streamline global instance structure and improve global val…
lum1n0us Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ tests/benchmarks/coremark/coremark*
samples/workload/include/**
!samples/workload/include/.gitkeep

# core/iwasm/libraries/wasi-threads
core/iwasm/libraries/lib-wasi-threads/**/*.aot
core/iwasm/libraries/lib-wasi-threads/**/*.wasm
3 changes: 2 additions & 1 deletion core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
#endif

#define AOT_MAGIC_NUMBER 0x746f6100
#define AOT_CURRENT_VERSION 3
/* FIXME: remove me when merged into main */
#define AOT_CURRENT_VERSION 4

#ifndef WASM_ENABLE_JIT
#define WASM_ENABLE_JIT 0
Expand Down
187 changes: 140 additions & 47 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ check_feature_flags(char *error_buf, uint32 error_buf_size,
}
#endif

#if WASM_ENABLE_MULTI_MODULE == 0
if (feature_flags & WASM_FEATURE_MULTI_MODULE) {
set_error_buf(error_buf, error_buf_size,
"multi-module is not enabled in this build");
return false;
}
#endif

return true;
}

Expand All @@ -529,7 +537,7 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, char *error_buf,
uint32 error_buf_size)
{
AOTTargetInfo target_info;
AOTTargetInfo target_info = { 0 };
const uint8 *p = buf, *p_end = buf_end;
bool is_target_little_endian, is_target_64_bit;

Expand Down Expand Up @@ -988,18 +996,17 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
{
const uint8 *buf = *p_buf;
AOTMemInitData **data_list;
uint64 size;
uint32 i;

/* Allocate memory */
size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
uint64 size =
sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
if (!(module->mem_init_data_list = data_list =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}

/* Create each memory data segment */
for (i = 0; i < module->mem_init_data_count; i++) {
for (uint32 i = 0; i < module->mem_init_data_count; i++) {
uint32 byte_count;
uint32 is_passive;
uint32 memory_index;
Expand Down Expand Up @@ -1036,26 +1043,57 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
}

static bool
load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
char *error_buf, uint32 error_buf_size)
load_import_memory_list(const uint8 **p_buf, const uint8 *buf_end,
AOTModule *module, bool is_load_from_file_buf,
char *error_buf, uint32 error_buf_size)
{
uint32 i;
uint64 total_size;
const uint8 *buf = *p_buf;

read_uint32(buf, buf_end, module->import_memory_count);
/* We don't support import_memory_count > 0 currently */
if (module->import_memory_count > 0)
uint64 size = sizeof(AOTImportMemory) * (uint64)module->import_memory_count;
AOTImportMemory *import_memories =
loader_malloc(size, error_buf, error_buf_size);
if (!import_memories) {
set_error_buf(error_buf, error_buf_size,
"allocate import_memories failed");
return false;
}

read_uint32(buf, buf_end, module->memory_count);
total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
module->import_memories = import_memories;
for (uint32 i = 0; i < module->import_memory_count; i++) {
AOTImportMemory *import_memory = import_memories + i;

read_uint32(buf, buf_end, import_memory->mem_type.flags);
if (!wasm_memory_check_flags(import_memory->mem_type.flags, error_buf,
error_buf_size, true)) {
set_error_buf(error_buf, error_buf_size, "invalid memory flags");
return false;
}

read_uint32(buf, buf_end, import_memory->mem_type.num_bytes_per_page);
read_uint32(buf, buf_end, import_memory->mem_type.init_page_count);
read_uint32(buf, buf_end, import_memory->mem_type.max_page_count);
read_string(buf, buf_end, import_memory->module_name);
read_string(buf, buf_end, import_memory->memory_name);
}

*p_buf = buf;
return true;
fail:
return false;
}

static bool
load_memory_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
char *error_buf, uint32 error_buf_size)
{
uint64 total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
if (!(module->memories =
loader_malloc(total_size, error_buf, error_buf_size))) {
return false;
}

for (i = 0; i < module->memory_count; i++) {
const uint8 *buf = *p_buf;
for (uint32 i = 0; i < module->memory_count; i++) {
read_uint32(buf, buf_end, module->memories[i].flags);

if (!wasm_memory_check_flags(module->memories[i].flags, error_buf,
Expand All @@ -1068,13 +1106,43 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
read_uint32(buf, buf_end, module->memories[i].max_page_count);
}

read_uint32(buf, buf_end, module->mem_init_data_count);
*p_buf = buf;
return true;
fail:
return false;
}

/* load memory init data list */
if (module->mem_init_data_count > 0
&& !load_mem_init_data_list(&buf, buf_end, module, error_buf,
error_buf_size))
return false;
static bool
load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
bool is_load_from_file_buf, char *error_buf,
uint32 error_buf_size)
{
const uint8 *buf = *p_buf;

read_uint32(buf, buf_end, module->import_memory_count);
if (module->import_memory_count > 0) {
if (!load_import_memory_list(&buf, buf_end, module,
is_load_from_file_buf, error_buf,
error_buf_size)) {
return false;
}
}

read_uint32(buf, buf_end, module->memory_count);
if (module->memory_count > 0) {
if (!load_memory_list(&buf, buf_end, module, error_buf,
error_buf_size)) {
return false;
}
}

read_uint32(buf, buf_end, module->mem_init_data_count);
if (module->mem_init_data_count > 0) {
if (!load_mem_init_data_list(&buf, buf_end, module, error_buf,
error_buf_size)) {
return false;
}
}

*p_buf = buf;
return true;
Expand Down Expand Up @@ -1305,8 +1373,8 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,

static bool
load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
AOTModule *module, char *error_buf,
uint32 error_buf_size)
AOTModule *module, bool is_load_from_file_buf,
char *error_buf, uint32 error_buf_size)
{
const uint8 *buf = *p_buf;
AOTImportTable *import_table;
Expand All @@ -1323,7 +1391,7 @@ load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
return false;
}

/* keep sync with aot_emit_table_info() aot_emit_aot_file */
/* keep sync with aot_emit_import_table_info() aot_emit_aot_file */
for (i = 0; i < module->import_table_count; i++, import_table++) {
read_uint8(buf, buf_end, import_table->table_type.elem_type);
read_uint8(buf, buf_end, import_table->table_type.flags);
Expand All @@ -1332,6 +1400,13 @@ load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
#else
{
/* TBC: placeholder */
uint8 placeholder;
read_uint8(buf, buf_end, placeholder);
(void)placeholder;
}
#endif
read_uint32(buf, buf_end, import_table->table_type.init_size);
read_uint32(buf, buf_end, import_table->table_type.max_size);
Expand All @@ -1350,6 +1425,12 @@ load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
}
}
#endif

/* v3 doesn't emit names */
if (module->package_version >= 4) {
read_string(buf, buf_end, import_table->module_name);
read_string(buf, buf_end, import_table->table_name);
}
}

*p_buf = buf;
Expand Down Expand Up @@ -1392,6 +1473,13 @@ load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
#else
{
/* TBC: placeholder */
uint8 placeholder;
read_uint8(buf, buf_end, placeholder);
(void)placeholder;
}
#endif
read_uint32(buf, buf_end, table->table_type.init_size);
read_uint32(buf, buf_end, table->table_type.max_size);
Expand Down Expand Up @@ -1523,14 +1611,15 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,

static bool
load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
char *error_buf, uint32 error_buf_size)
bool is_load_from_file_buf, char *error_buf,
uint32 error_buf_size)
{
const uint8 *buf = *p_buf;

read_uint32(buf, buf_end, module->import_table_count);
if (module->import_table_count > 0
&& !load_import_table_list(&buf, buf_end, module, error_buf,
error_buf_size))
&& !load_import_table_list(&buf, buf_end, module, is_load_from_file_buf,
error_buf, error_buf_size))
return false;

read_uint32(buf, buf_end, module->table_count);
Expand Down Expand Up @@ -2084,24 +2173,26 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
return false;
}

#if WASM_ENABLE_LIBC_BUILTIN != 0
if (wasm_native_lookup_libc_builtin_global(
import_globals[i].module_name, import_globals[i].global_name,
&tmp_global)) {
if (tmp_global.type.val_type != import_globals[i].type.val_type
|| tmp_global.type.is_mutable
!= import_globals[i].type.is_mutable) {
set_error_buf(error_buf, error_buf_size,
"incompatible import type");
return false;
}
import_globals[i].global_data_linked =
tmp_global.global_data_linked;
import_globals[i].is_linked = true;
}
#else
import_globals[i].is_linked = false;
#endif
/*TODO: move to built-in linker */
// #if WASM_ENABLE_LIBC_BUILTIN != 0
// if (wasm_native_lookup_libc_builtin_global(
// import_globals[i].module_name,
// import_globals[i].global_name, &tmp_global)) {
// if (tmp_global.type.val_type !=
// import_globals[i].type.val_type
// || tmp_global.type.is_mutable
// != import_globals[i].type.is_mutable) {
// set_error_buf(error_buf, error_buf_size,
// "incompatible import type");
// return false;
// }
// import_globals[i].global_data_linked =
// tmp_global.global_data_linked;
// import_globals[i].is_linked = true;
// }
// #else
// import_globals[i].is_linked = false;
// #endif

import_globals[i].size =
wasm_value_type_size(import_globals[i].type.val_type);
Expand Down Expand Up @@ -2414,8 +2505,10 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end,
{
const uint8 *p = buf, *p_end = buf_end;

if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
|| !load_table_info(&p, p_end, module, error_buf, error_buf_size)
if (!load_memory_info(&p, p_end, module, is_load_from_file_buf, error_buf,
error_buf_size)
|| !load_table_info(&p, p_end, module, is_load_from_file_buf, error_buf,
error_buf_size)
|| !load_type_info(&p, p_end, module, error_buf, error_buf_size)
|| !load_import_global_info(&p, p_end, module, is_load_from_file_buf,
error_buf, error_buf_size)
Expand Down
Loading
Loading