Refactor interpreter/AOT module instance layout (#1559)

Refactor the layout of interpreter and AOT module instance:
- Unify the interp/AOT module instance, use the same WASMModuleInstance/
  WASMMemoryInstance/WASMTableInstance data structures for both interpreter
  and AOT
- Make the offset of most fields the same in module instance for both interpreter
  and AOT, append memory instance structure, global data and table instances to
  the end of module instance for interpreter mode (like AOT mode)
- For extra fields in WASM module instance, use WASMModuleInstanceExtra to
  create a field `e` for interpreter
- Change the LLVM JIT module instance creating process, LLVM JIT uses the WASM
  module and module instance same as interpreter/Fast-JIT mode. So that Fast JIT
  and LLVM JIT can access the same data structures, and make it possible to
  implement the Multi-tier JIT (tier-up from Fast JIT to LLVM JIT) in the future
- Unify some APIs: merge some APIs for module instance and memory instance's
  related operations (only implement one copy)

Note that the AOT ABI is same, the AOT file format, AOT relocation types, how AOT
code accesses the AOT module instance and so on are kept unchanged.

Refer to:
https://github.com/bytecodealliance/wasm-micro-runtime/issues/1384
This commit is contained in:
Wenyong Huang
2022-10-18 10:59:28 +08:00
committed by GitHub
parent dc4dcc3d6f
commit a182926a73
49 changed files with 3790 additions and 3274 deletions

View File

@ -10,34 +10,11 @@
#include "../common/wasm_runtime_common.h"
#include "../interpreter/wasm_runtime.h"
#include "../compilation/aot.h"
#if WASM_ENABLE_JIT != 0
#include "../compilation/aot_llvm.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef enum AOTExceptionID {
EXCE_UNREACHABLE = 0,
EXCE_OUT_OF_MEMORY,
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
EXCE_INTEGER_OVERFLOW,
EXCE_INTEGER_DIVIDE_BY_ZERO,
EXCE_INVALID_CONVERSION_TO_INTEGER,
EXCE_INVALID_FUNCTION_TYPE_INDEX,
EXCE_INVALID_FUNCTION_INDEX,
EXCE_UNDEFINED_ELEMENT,
EXCE_UNINITIALIZED_ELEMENT,
EXCE_CALL_UNLINKED_IMPORT_FUNC,
EXCE_NATIVE_STACK_OVERFLOW,
EXCE_UNALIGNED_ATOMIC,
EXCE_AUX_STACK_OVERFLOW,
EXCE_AUX_STACK_UNDERFLOW,
EXCE_OUT_OF_BOUNDS_TABLE_ACCESS,
EXCE_NUM,
} AOTExceptionID;
typedef enum AOTSectionType {
AOT_SECTION_TYPE_TARGET_INFO = 0,
AOT_SECTION_TYPE_INIT_DATA = 1,
@ -171,7 +148,7 @@ typedef struct AOTModule {
/* function info */
uint32 func_count;
/* point to AOTed/JITed functions */
/* point to AOTed functions */
void **func_ptrs;
/* function type indexes */
uint32 *func_type_indexes;
@ -182,24 +159,24 @@ typedef struct AOTModule {
/* start function index, -1 denotes no start function */
uint32 start_func_index;
/* start function, point to AOTed/JITed function */
/* start function, point to AOTed function */
void *start_function;
uint32 malloc_func_index;
uint32 free_func_index;
uint32 retain_func_index;
/* AOTed code, NULL for JIT mode */
/* AOTed code */
void *code;
uint32 code_size;
/* literal for AOTed code, NULL for JIT mode */
/* literal for AOTed code */
uint8 *literal;
uint32 literal_size;
#if defined(BH_PLATFORM_WINDOWS)
/* extra plt data area for __ymm, __xmm and __real constants
in Windows platform, NULL for JIT mode */
in Windows platform */
uint8 *extra_plt_data;
uint32 extra_plt_data_size;
uint32 ymm_plt_count;
@ -217,7 +194,7 @@ typedef struct AOTModule {
#endif
/* data sections in AOT object file, including .data, .rodata
* and .rodata.cstN. NULL for JIT mode. */
and .rodata.cstN. */
AOTObjectDataSection *data_sections;
uint32 data_section_count;
@ -244,18 +221,9 @@ typedef struct AOTModule {
/* auxiliary stack size resolved */
uint32 aux_stack_size;
/* is jit mode or not */
bool is_jit_mode;
/* is indirect mode or not */
bool is_indirect_mode;
#if WASM_ENABLE_JIT != 0
WASMModule *wasm_module;
AOTCompContext *comp_ctx;
AOTCompData *comp_data;
#endif
#if WASM_ENABLE_LIBC_WASI != 0
WASIArguments wasi_args;
bool import_wasi_api;
@ -274,130 +242,9 @@ typedef struct AOTModule {
#endif
} AOTModule;
typedef union {
uint64 _make_it_8_bytes_;
void *ptr;
} AOTPointer;
typedef union {
uint64 u64;
uint32 u32[2];
} MemBound;
typedef struct AOTMemoryInstance {
uint32 module_type;
/* shared memory flag */
bool is_shared;
/* memory space info */
uint32 num_bytes_per_page;
uint32 cur_page_count;
uint32 max_page_count;
uint32 memory_data_size;
AOTPointer memory_data;
AOTPointer memory_data_end;
/* heap space info */
AOTPointer heap_data;
AOTPointer heap_data_end;
AOTPointer heap_handle;
/* boundary check constants for aot code */
MemBound mem_bound_check_1byte;
MemBound mem_bound_check_2bytes;
MemBound mem_bound_check_4bytes;
MemBound mem_bound_check_8bytes;
MemBound mem_bound_check_16bytes;
} AOTMemoryInstance;
typedef struct AOTTableInstance {
uint32 cur_size;
/*
* only grow in the range of [:max_size)
* if the table is growable, max_size equals to its declared maximum size
* otherwise, max_size equals to its declared minimum size
*/
uint32 max_size;
/*
* +------------------------------+ <--- data
* | ref.func[] or ref.extern[]
* +------------------------------+
*/
uint32 data[1];
} AOTTableInstance;
typedef struct AOTModuleInstance {
uint32 module_type;
/* memories */
uint32 memory_count;
AOTPointer memories;
/* global and table info */
uint32 global_data_size;
/*
* the count of AOTTableInstance.
* it includes imported tables and local tables.
*
* TODO: for now we treate imported table like a local table
*/
uint32 table_count;
/* points to global_data */
AOTPointer global_data;
/* points to AOTTableInstance[] */
AOTPointer tables;
/* function pointer array */
AOTPointer func_ptrs;
/* function type indexes */
AOTPointer func_type_indexes;
/* export info */
uint32 export_func_count;
uint32 export_global_count;
uint32 export_mem_count;
uint32 export_tab_count;
AOTPointer export_funcs;
AOTPointer export_globals;
AOTPointer export_memories;
AOTPointer export_tables;
/* The exception buffer for current thread. */
char cur_exception[128];
/* The custom data that can be set/get by
* wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
AOTPointer custom_data;
/* The AOT module */
AOTPointer aot_module;
/* WASI context */
AOTPointer wasi_ctx;
/* function performance profiling info list */
AOTPointer func_perf_profilings;
/* stack frames, used in call stack dump and perf profiling */
AOTPointer frames;
AOTPointer exec_env_singleton;
uint32 default_wasm_stack_size;
/* reserved */
uint32 reserved[9];
/*
* +------------------------------+ <-- memories.ptr
* | #0 AOTMemoryInstance
* +------------------------------+ <-- global_data.ptr
* | global data
* +------------------------------+ <-- tables.ptr
* | AOTTableInstance[table_count]
* +------------------------------+
*/
union {
uint64 _make_it_8_byte_aligned_;
AOTMemoryInstance memory_instances[1];
uint8 bytes[1];
} global_table_data;
} AOTModuleInstance;
#define AOTMemoryInstance WASMMemoryInstance
#define AOTTableInstance WASMTableInstance
#define AOTModuleInstance WASMModuleInstance
/* Target info, read from ELF header of object file */
typedef struct AOTTargetInfo {
@ -462,21 +309,6 @@ AOTModule *
aot_load_from_sections(AOTSection *section_list, char *error_buf,
uint32 error_buf_size);
#if WASM_ENABLE_JIT != 0
/**
* Convert WASM module to AOT module
*
* @param wasm_module the WASM module to convert
* @param error_buf output of the error info
* @param error_buf_size the size of the error string
*
* @return return AOT module loaded, NULL if failed
*/
AOTModule *
aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf,
uint32 error_buf_size);
#endif
/**
* Unload a AOT module.
*
@ -549,9 +381,6 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
AOTFunctionInstance *function,
unsigned argc, uint32 argv[]);
bool
aot_create_exec_env_singleton(AOTModuleInstance *module_inst);
/**
* Set AOT module instance exception with exception string
*
@ -575,14 +404,6 @@ aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id);
const char *
aot_get_exception(AOTModuleInstance *module_inst);
/**
* Clear exception info of the AOT module instance.
*
* @param module_inst the AOT module instance
*/
void
aot_clear_exception(AOTModuleInstance *module_inst);
uint32
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
void **p_native_addr);
@ -598,29 +419,6 @@ uint32
aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
uint32 size);
bool
aot_validate_app_addr(AOTModuleInstance *module_inst, uint32 app_offset,
uint32 size);
bool
aot_validate_native_addr(AOTModuleInstance *module_inst, void *native_ptr,
uint32 size);
void *
aot_addr_app_to_native(AOTModuleInstance *module_inst, uint32 app_offset);
uint32
aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr);
bool
aot_get_app_addr_range(AOTModuleInstance *module_inst, uint32 app_offset,
uint32 *p_app_start_offset, uint32 *p_app_end_offset);
bool
aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr,
uint8 **p_native_start_addr,
uint8 **p_native_end_addr);
bool
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
@ -635,6 +433,10 @@ bool
aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
uint32 argc, uint32 *argv);
/**
* Check whether the app address and the buf is inside the linear memory,
* and convert the app address into native address
*/
bool
aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
uint32 app_buf_addr, uint32 app_buf_size,
@ -666,16 +468,6 @@ bool
aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
#endif
#ifdef OS_ENABLE_HW_BOUND_CHECK
#ifndef BH_PLATFORM_WINDOWS
void
aot_signal_handler(WASMSignalInfo *sig_info);
#else
LONG
aot_exception_handler(WASMSignalInfo *sig_info);
#endif
#endif
void
aot_get_module_mem_consumption(const AOTModule *module,
WASMModuleMemConsumption *mem_conspn);
@ -707,9 +499,6 @@ aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
uint32 inc_entries, uint32 init_val);
#endif
AOTTableInstance *
aot_next_tbl_inst(const AOTTableInstance *tbl_inst);
bool
aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);