Skip to content

Commit 58bbb7c

Browse files
authored
Unify wasm module instance and aot module instance (#1495)
Unify the data structure of wasm module instance and aot module instance: - Use the same structure WASMModuleInstance for both interpreter and AOT - For the extra fields, store them into WASMModuleInstanceExtra structure and add a field `e` in module instance - For field which has different meaning for interpreter and AOT, use the structure of interpreter to define it, and convert it to actual type in AOT before using it Refer to #1384.
1 parent 2997b39 commit 58bbb7c

17 files changed

+745
-798
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 173 additions & 176 deletions
Large diffs are not rendered by default.

core/iwasm/aot/aot_runtime.h

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -262,126 +262,9 @@ typedef struct AOTModule {
262262
#endif
263263
} AOTModule;
264264

265-
typedef union {
266-
uint64 _make_it_8_bytes_;
267-
void *ptr;
268-
} AOTPointer;
269-
270-
typedef struct AOTMemoryInstance {
271-
uint32 module_type;
272-
/* shared memory flag */
273-
bool is_shared;
274-
275-
/* memory space info */
276-
uint32 num_bytes_per_page;
277-
uint32 cur_page_count;
278-
uint32 max_page_count;
279-
uint32 memory_data_size;
280-
AOTPointer memory_data;
281-
AOTPointer memory_data_end;
282-
283-
/* heap space info */
284-
AOTPointer heap_data;
285-
AOTPointer heap_data_end;
286-
AOTPointer heap_handle;
287-
288-
/* boundary check constants for aot code */
289-
MemBound mem_bound_check_1byte;
290-
MemBound mem_bound_check_2bytes;
291-
MemBound mem_bound_check_4bytes;
292-
MemBound mem_bound_check_8bytes;
293-
MemBound mem_bound_check_16bytes;
294-
} AOTMemoryInstance;
295-
296-
typedef struct AOTTableInstance {
297-
uint32 cur_size;
298-
/*
299-
* only grow in the range of [:max_size)
300-
* if the table is growable, max_size equals to its declared maximum size
301-
* otherwise, max_size equals to its declared minimum size
302-
*/
303-
uint32 max_size;
304-
/*
305-
* +------------------------------+ <--- data
306-
* | ref.func[] or ref.extern[]
307-
* +------------------------------+
308-
*/
309-
uint32 data[1];
310-
} AOTTableInstance;
311-
312-
typedef struct AOTModuleInstance {
313-
uint32 module_type;
314-
315-
/* memories */
316-
uint32 memory_count;
317-
AOTPointer memories;
318-
319-
/* global and table info */
320-
uint32 global_data_size;
321-
/*
322-
* the count of AOTTableInstance.
323-
* it includes imported tables and local tables.
324-
*
325-
* TODO: for now we treate imported table like a local table
326-
*/
327-
uint32 table_count;
328-
/* points to global_data */
329-
AOTPointer global_data;
330-
/* points to AOTTableInstance[] */
331-
AOTPointer tables;
332-
333-
/* function pointer array */
334-
AOTPointer func_ptrs;
335-
/* function type indexes */
336-
AOTPointer func_type_indexes;
337-
338-
/* export info */
339-
uint32 export_func_count;
340-
uint32 export_global_count;
341-
uint32 export_memory_count;
342-
uint32 export_table_count;
343-
AOTPointer export_funcs;
344-
AOTPointer export_globals;
345-
AOTPointer export_memories;
346-
AOTPointer export_tables;
347-
348-
/* The exception buffer for current thread. */
349-
char cur_exception[128];
350-
351-
/* The custom data that can be set/get by
352-
* wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
353-
AOTPointer custom_data;
354-
/* The AOT module */
355-
AOTPointer aot_module;
356-
/* WASI context */
357-
AOTPointer wasi_ctx;
358-
/* function performance profiling info list */
359-
AOTPointer func_perf_profilings;
360-
/* stack frames, used in call stack dump and perf profiling */
361-
AOTPointer frames;
362-
363-
AOTPointer exec_env_singleton;
364-
365-
uint32 default_wasm_stack_size;
366-
367-
/* reserved */
368-
uint32 reserved[9];
369-
370-
/*
371-
* +------------------------------+ <-- memories.ptr
372-
* | #0 AOTMemoryInstance
373-
* +------------------------------+ <-- global_data.ptr
374-
* | global data
375-
* +------------------------------+ <-- tables.ptr
376-
* | AOTTableInstance[table_count]
377-
* +------------------------------+
378-
*/
379-
union {
380-
uint64 _make_it_8_byte_aligned_;
381-
AOTMemoryInstance memory_instances[1];
382-
uint8 bytes[1];
383-
} global_table_data;
384-
} AOTModuleInstance;
265+
#define AOTMemoryInstance WASMMemoryInstance
266+
#define AOTTableInstance WASMTableInstance
267+
#define AOTModuleInstance WASMModuleInstance
385268

386269
/* Target info, read from ELF header of object file */
387270
typedef struct AOTTargetInfo {

core/iwasm/common/wasm_c_api.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt,
16551655

16561656
#if WASM_ENABLE_AOT != 0
16571657
if (inst_comm_rt->module_type == Wasm_Module_AoT) {
1658-
trap->frames = ((AOTModuleInstance *)inst_comm_rt)->frames.ptr;
1658+
trap->frames = ((AOTModuleInstance *)inst_comm_rt)->frames;
16591659
}
16601660
#endif
16611661
#endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
@@ -2482,9 +2482,9 @@ wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
24822482
#if WASM_ENABLE_INTERP != 0
24832483
if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
24842484
bh_assert(func_idx_rt
2485-
< ((WASMModuleInstance *)inst_comm_rt)->function_count);
2485+
< ((WASMModuleInstance *)inst_comm_rt)->e->function_count);
24862486
WASMFunctionInstance *func_interp =
2487-
((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt;
2487+
((WASMModuleInstance *)inst_comm_rt)->e->functions + func_idx_rt;
24882488
type_rt = func_interp->is_import_func
24892489
? func_interp->u.func_import->func_type
24902490
: func_interp->u.func->func_type;
@@ -2496,7 +2496,7 @@ wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
24962496
/* use same index to trace the function type in AOTFuncType **func_types
24972497
*/
24982498
AOTModule *module_aot =
2499-
((AOTModuleInstance *)inst_comm_rt)->aot_module.ptr;
2499+
(AOTModule *)((AOTModuleInstance *)inst_comm_rt)->module;
25002500
if (func_idx_rt < module_aot->import_func_count) {
25012501
type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
25022502
}
@@ -2748,7 +2748,7 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
27482748

27492749
#if WASM_ENABLE_INTERP != 0
27502750
if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
2751-
func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->functions
2751+
func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->e->functions
27522752
+ func->func_idx_rt;
27532753
}
27542754
#endif
@@ -2758,15 +2758,15 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
27582758
if (!(func_comm_rt = func->func_comm_rt)) {
27592759
AOTModuleInstance *inst_aot =
27602760
(AOTModuleInstance *)func->inst_comm_rt;
2761-
AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
2761+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
27622762
uint32 export_i = 0, export_func_j = 0;
27632763

27642764
for (; export_i < module_aot->export_count; ++export_i) {
27652765
AOTExport *export = module_aot->exports + export_i;
27662766
if (export->kind == EXPORT_KIND_FUNC) {
27672767
if (export->index == func->func_idx_rt) {
27682768
func_comm_rt =
2769-
(AOTFunctionInstance *)inst_aot->export_funcs.ptr
2769+
(AOTFunctionInstance *)inst_aot->export_functions
27702770
+ export_func_j;
27712771
((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
27722772
break;
@@ -2968,7 +2968,7 @@ interp_global_set(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
29682968
const wasm_val_t *v)
29692969
{
29702970
const WASMGlobalInstance *global_interp =
2971-
inst_interp->globals + global_idx_rt;
2971+
inst_interp->e->globals + global_idx_rt;
29722972
uint8 val_type_rt = global_interp->type;
29732973
#if WASM_ENABLE_MULTI_MODULE != 0
29742974
uint8 *data = global_interp->import_global_inst
@@ -2987,7 +2987,7 @@ static bool
29872987
interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
29882988
wasm_val_t *out)
29892989
{
2990-
WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt;
2990+
WASMGlobalInstance *global_interp = inst_interp->e->globals + global_idx_rt;
29912991
uint8 val_type_rt = global_interp->type;
29922992
#if WASM_ENABLE_MULTI_MODULE != 0
29932993
uint8 *data = global_interp->import_global_inst
@@ -3007,7 +3007,7 @@ static bool
30073007
aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
30083008
const wasm_val_t *v)
30093009
{
3010-
AOTModule *module_aot = inst_aot->aot_module.ptr;
3010+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
30113011
uint8 val_type_rt = 0;
30123012
uint32 data_offset = 0;
30133013
void *data = NULL;
@@ -3025,7 +3025,7 @@ aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
30253025
.type;
30263026
}
30273027

3028-
data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
3028+
data = (void *)(inst_aot->global_data + data_offset);
30293029
return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt,
30303030
v, data);
30313031
}
@@ -3034,7 +3034,7 @@ static bool
30343034
aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
30353035
wasm_val_t *out)
30363036
{
3037-
AOTModule *module_aot = inst_aot->aot_module.ptr;
3037+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
30383038
uint8 val_type_rt = 0;
30393039
uint32 data_offset = 0;
30403040
uint8 *data = NULL;
@@ -3052,7 +3052,7 @@ aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
30523052
.type;
30533053
}
30543054

3055-
data = (uint8 *)inst_aot->global_data.ptr + data_offset;
3055+
data = inst_aot->global_data + data_offset;
30563056
return rt_val_to_wasm_val(data, val_type_rt, out);
30573057
}
30583058
#endif
@@ -3149,7 +3149,7 @@ wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
31493149
#if WASM_ENABLE_INTERP != 0
31503150
if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
31513151
WASMGlobalInstance *global_interp =
3152-
((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt;
3152+
((WASMModuleInstance *)inst_comm_rt)->e->globals + global_idx_rt;
31533153
val_type_rt = global_interp->type;
31543154
is_mutable = global_interp->is_mutable;
31553155
init = true;
@@ -3159,7 +3159,7 @@ wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
31593159
#if WASM_ENABLE_AOT != 0
31603160
if (inst_comm_rt->module_type == Wasm_Module_AoT) {
31613161
AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
3162-
AOTModule *module_aot = inst_aot->aot_module.ptr;
3162+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
31633163

31643164
init = true;
31653165

@@ -3365,19 +3365,19 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
33653365
if (index >= table_interp->cur_size) {
33663366
return NULL;
33673367
}
3368-
ref_idx = ((uint32 *)table_interp->base_addr)[index];
3368+
ref_idx = table_interp->elems[index];
33693369
}
33703370
#endif
33713371

33723372
#if WASM_ENABLE_AOT != 0
33733373
if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
33743374
AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
33753375
AOTTableInstance *table_aot =
3376-
(AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt;
3376+
(AOTTableInstance *)inst_aot->tables + table->table_idx_rt;
33773377
if (index >= table_aot->cur_size) {
33783378
return NULL;
33793379
}
3380-
ref_idx = table_aot->data[index];
3380+
ref_idx = table_aot->elems[index];
33813381
}
33823382
#endif
33833383

@@ -3437,24 +3437,24 @@ wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
34373437
return false;
34383438
}
34393439

3440-
p_ref_idx = ((uint32 *)table_interp->base_addr) + index;
3440+
p_ref_idx = table_interp->elems + index;
34413441
function_count =
3442-
((WASMModuleInstance *)table->inst_comm_rt)->function_count;
3442+
((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
34433443
}
34443444
#endif
34453445

34463446
#if WASM_ENABLE_AOT != 0
34473447
if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
34483448
AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
3449-
AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
3449+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
34503450
AOTTableInstance *table_aot =
3451-
(AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt;
3451+
(AOTTableInstance *)inst_aot->tables + table->table_idx_rt;
34523452

34533453
if (index >= table_aot->cur_size) {
34543454
return false;
34553455
}
34563456

3457-
p_ref_idx = table_aot->data + index;
3457+
p_ref_idx = table_aot->elems + index;
34583458
function_count = module_aot->func_count;
34593459
}
34603460
#endif
@@ -3510,7 +3510,7 @@ wasm_table_size(const wasm_table_t *table)
35103510
#if WASM_ENABLE_AOT != 0
35113511
if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
35123512
AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
3513-
AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
3513+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
35143514

35153515
if (table->table_idx_rt < module_aot->import_table_count) {
35163516
AOTImportTable *table_aot =
@@ -3625,7 +3625,7 @@ wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
36253625
#if WASM_ENABLE_AOT != 0
36263626
if (inst_comm_rt->module_type == Wasm_Module_AoT) {
36273627
AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
3628-
AOTModule *module_aot = (AOTModule *)(inst_aot->aot_module.ptr);
3628+
AOTModule *module_aot = (AOTModule *)inst_aot->module;
36293629

36303630
if (memory_idx_rt < module_aot->import_memory_count) {
36313631
min_pages = module_aot->import_memories->mem_init_page_count;
@@ -3709,8 +3709,8 @@ wasm_memory_data(wasm_memory_t *memory)
37093709
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
37103710
AOTMemoryInstance *memory_inst =
37113711
((AOTMemoryInstance **)
3712-
module_inst->memories.ptr)[memory->memory_idx_rt];
3713-
return (byte_t *)memory_inst->memory_data.ptr;
3712+
module_inst->memories)[memory->memory_idx_rt];
3713+
return (byte_t *)memory_inst->memory_data;
37143714
}
37153715
#endif
37163716

@@ -3746,7 +3746,7 @@ wasm_memory_data_size(const wasm_memory_t *memory)
37463746
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
37473747
AOTMemoryInstance *memory_inst =
37483748
((AOTMemoryInstance **)
3749-
module_inst->memories.ptr)[memory->memory_idx_rt];
3749+
module_inst->memories)[memory->memory_idx_rt];
37503750
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
37513751
}
37523752
#endif
@@ -3783,7 +3783,7 @@ wasm_memory_size(const wasm_memory_t *memory)
37833783
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
37843784
AOTMemoryInstance *memory_inst =
37853785
((AOTMemoryInstance **)
3786-
module_inst->memories.ptr)[memory->memory_idx_rt];
3786+
module_inst->memories)[memory->memory_idx_rt];
37873787
return memory_inst->cur_page_count;
37883788
}
37893789
#endif
@@ -4162,7 +4162,7 @@ aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
41624162

41634163
bh_assert(store && inst_aot && externals);
41644164

4165-
module_aot = (AOTModule *)inst_aot->aot_module.ptr;
4165+
module_aot = (AOTModule *)inst_aot->module;
41664166
bh_assert(module_aot);
41674167

41684168
for (i = 0; i < module_aot->export_count; ++i) {

core/iwasm/common/wasm_exec_env.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
6565
#if WASM_ENABLE_AOT != 0
6666
if (module_inst->module_type == Wasm_Module_AoT) {
6767
AOTModuleInstance *i = (AOTModuleInstance *)module_inst;
68-
AOTModule *m = (AOTModule *)i->aot_module.ptr;
68+
AOTModule *m = (AOTModule *)i->module;
6969
exec_env->native_symbol = m->native_symbol_list;
7070
}
7171
#endif
@@ -135,7 +135,7 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
135135
/* Set the aux_stack_boundary and aux_stack_bottom */
136136
if (module_inst->module_type == Wasm_Module_AoT) {
137137
AOTModule *module =
138-
(AOTModule *)(((AOTModuleInstance *)module_inst)->aot_module.ptr);
138+
(AOTModule *)((AOTModuleInstance *)module_inst)->module;
139139
exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom;
140140
exec_env->aux_stack_boundary.boundary =
141141
module->aux_stack_bottom - module->aux_stack_size;

0 commit comments

Comments
 (0)