Skip to content

Commit 256ecdf

Browse files
authored
Refine interpreter to improve performance, refine memory usage (#161)
1 parent 130d7d0 commit 256ecdf

File tree

26 files changed

+486
-248
lines changed

26 files changed

+486
-248
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
289289
const uint8 *p = buf, *p_end = buf_end;
290290
bool is_target_little_endian, is_target_64_bit;
291291

292-
read_uint32(p, p_end, target_info.bin_type);
292+
read_uint16(p, p_end, target_info.bin_type);
293+
read_uint16(p, p_end, target_info.abi_type);
293294
read_uint16(p, p_end, target_info.e_type);
294295
read_uint16(p, p_end, target_info.e_machine);
295296
read_uint32(p, p_end, target_info.e_version);
@@ -424,6 +425,7 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end,
424425
{
425426
const uint8 *buf = *p_buf;
426427

428+
read_uint32(buf, buf_end, module->num_bytes_per_page);
427429
read_uint32(buf, buf_end, module->mem_init_page_count);
428430
read_uint32(buf, buf_end, module->mem_max_page_count);
429431
read_uint32(buf, buf_end, module->mem_init_data_count);
@@ -1001,6 +1003,11 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end,
10011003
return false;
10021004
}
10031005

1006+
read_uint32(p, p_end, module->llvm_aux_data_end);
1007+
read_uint32(p, p_end, module->llvm_aux_stack_bottom);
1008+
read_uint32(p, p_end, module->llvm_aux_stack_size);
1009+
read_uint32(p, p_end, module->llvm_aux_stack_global_index);
1010+
10041011
if (!load_object_data_sections_info(&p, p_end, module,
10051012
error_buf, error_buf_size))
10061013
return false;
@@ -2297,6 +2304,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
22972304
memset(module, 0, sizeof(AOTModule));
22982305

22992306
module->module_type = Wasm_Module_AoT;
2307+
module->num_bytes_per_page = comp_data->num_bytes_per_page;
23002308
module->mem_init_page_count = comp_data->mem_init_page_count;
23012309
module->mem_max_page_count = comp_data->mem_max_page_count;
23022310

@@ -2382,6 +2390,11 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
23822390
module->start_function = NULL;
23832391
}
23842392

2393+
module->llvm_aux_data_end = comp_data->llvm_aux_data_end;
2394+
module->llvm_aux_stack_bottom = comp_data->llvm_aux_stack_bottom;
2395+
module->llvm_aux_stack_size = comp_data->llvm_aux_stack_size;
2396+
module->llvm_aux_stack_global_index = comp_data->llvm_aux_stack_global_index;
2397+
23852398
module->code = NULL;
23862399
module->code_size = 0;
23872400

core/iwasm/aot/aot_runtime.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
111111
{
112112
uint32 i, global_index, global_data_offset, base_offset, length;
113113
AOTMemInitData *data_seg;
114-
uint64 total_size = (uint64)NumBytesPerPage * module->mem_init_page_count;
114+
uint64 total_size = (uint64)module->num_bytes_per_page * module->mem_init_page_count;
115115

116116
/* Allocate memory */
117117
if (total_size >= UINT32_MAX
@@ -802,11 +802,13 @@ bool
802802
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
803803
{
804804
uint8 *mem_data_old = module_inst->memory_data.ptr, *mem_data_new;
805+
uint32 num_bytes_per_page =
806+
((AOTModule*)module_inst->aot_module.ptr)->num_bytes_per_page;
805807
uint32 cur_page_count = module_inst->mem_cur_page_count;
806808
uint32 max_page_count = module_inst->mem_max_page_count;
807809
uint32 total_page_count = cur_page_count + inc_page_count;
808-
uint32 old_size = NumBytesPerPage * cur_page_count;
809-
uint64 total_size = (uint64)NumBytesPerPage * total_page_count;
810+
uint32 old_size = num_bytes_per_page * cur_page_count;
811+
uint64 total_size = (uint64)num_bytes_per_page * total_page_count;
810812

811813
if (inc_page_count <= 0)
812814
/* No need to enlarge memory */

core/iwasm/aot/aot_runtime.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef struct AOTModule {
7575
uint32 module_type;
7676

7777
/* memory info */
78+
uint32 num_bytes_per_page;
7879
uint32 mem_init_page_count;
7980
uint32 mem_max_page_count;
8081
uint32 mem_init_data_count;
@@ -132,6 +133,11 @@ typedef struct AOTModule {
132133
/* constant string set */
133134
HashMap *const_str_set;
134135

136+
uint32 llvm_aux_data_end;
137+
uint32 llvm_aux_stack_bottom;
138+
uint32 llvm_aux_stack_size;
139+
uint32 llvm_aux_stack_global_index;
140+
135141
/* is jit mode or not */
136142
bool is_jit_mode;
137143

@@ -210,7 +216,9 @@ typedef AOTExportFunc AOTFunctionInstance;
210216
/* Target info, read from ELF header of object file */
211217
typedef struct AOTTargetInfo {
212218
/* Binary type, elf32l/elf32b/elf64l/elf64b */
213-
uint32 bin_type;
219+
uint16 bin_type;
220+
/* ABI type */
221+
uint16 abi_type;
214222
/* Object file type */
215223
uint16 e_type;
216224
/* Architecture */

core/iwasm/compilation/aot.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,16 @@ aot_create_comp_data(WASMModule *module)
384384

385385
/* Set memory page count */
386386
if (module->import_memory_count) {
387+
comp_data->num_bytes_per_page =
388+
module->import_memories[0].u.memory.num_bytes_per_page;
387389
comp_data->mem_init_page_count =
388390
module->import_memories[0].u.memory.init_page_count;
389391
comp_data->mem_max_page_count =
390392
module->import_memories[0].u.memory.max_page_count;
391393
}
392394
else if (module->memory_count) {
395+
comp_data->num_bytes_per_page =
396+
module->memories[0].num_bytes_per_page;
393397
comp_data->mem_init_page_count =
394398
module->memories[0].init_page_count;
395399
comp_data->mem_max_page_count =

core/iwasm/compilation/aot.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ typedef struct AOTExportFunc {
110110

111111
typedef struct AOTCompData {
112112
/* Memory and memory init data info */
113+
uint32 num_bytes_per_page;
113114
uint32 mem_init_page_count;
114115
uint32 mem_max_page_count;
115116
uint32 mem_init_data_count;
@@ -142,6 +143,11 @@ typedef struct AOTCompData {
142143
uint32 addr_data_size;
143144
uint32 global_data_size;
144145

146+
uint32 llvm_aux_data_end;
147+
uint32 llvm_aux_stack_bottom;
148+
uint32 llvm_aux_stack_size;
149+
uint32 llvm_aux_stack_global_index;
150+
145151
WASMModule *wasm_module;
146152
} AOTCompData;
147153

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ get_mem_init_data_list_size(AOTMemInitData **mem_init_data_list,
141141
static uint32
142142
get_mem_info_size(AOTCompData *comp_data)
143143
{
144-
/* init page count + max page count + init data count + init data list */
145-
return (uint32)sizeof(uint32) * 3
144+
/* num bytes per page + init page count + max page count
145+
+ init data count + init data list */
146+
return (uint32)sizeof(uint32) * 4
146147
+ get_mem_init_data_list_size(comp_data->mem_init_data_list,
147148
comp_data->mem_init_data_count);
148149
}
@@ -367,6 +368,10 @@ get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data)
367368
size = align_uint(size, 4);
368369
size += (uint32)sizeof(uint32) * 2;
369370

371+
/* llvm aux data end + llvm aux stack bottom
372+
+ llvm aux stack size + llvm stack global index */
373+
size += sizeof(uint32) * 4;
374+
370375
size += get_object_data_section_info_size(obj_data);
371376
return size;
372377
}
@@ -837,7 +842,8 @@ aot_emit_target_info_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
837842
EMIT_U32(AOT_SECTION_TYPE_TARGET_INFO);
838843
EMIT_U32(section_size);
839844

840-
EMIT_U32(target_info->bin_type);
845+
EMIT_U16(target_info->bin_type);
846+
EMIT_U16(target_info->abi_type);
841847
EMIT_U16(target_info->e_type);
842848
EMIT_U16(target_info->e_machine);
843849
EMIT_U32(target_info->e_version);
@@ -864,6 +870,7 @@ aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
864870

865871
*p_offset = offset = align_uint(offset, 4);
866872

873+
EMIT_U32(comp_data->num_bytes_per_page);
867874
EMIT_U32(comp_data->mem_init_page_count);
868875
EMIT_U32(comp_data->mem_max_page_count);
869876
EMIT_U32(comp_data->mem_init_data_count);
@@ -1087,6 +1094,11 @@ aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
10871094
EMIT_U32(comp_data->func_count);
10881095
EMIT_U32(comp_data->start_func_index);
10891096

1097+
EMIT_U32(comp_data->llvm_aux_data_end);
1098+
EMIT_U32(comp_data->llvm_aux_stack_bottom);
1099+
EMIT_U32(comp_data->llvm_aux_stack_size);
1100+
EMIT_U32(comp_data->llvm_aux_stack_global_index);
1101+
10901102
if (!aot_emit_object_data_section_info(buf, buf_end, &offset, obj_data))
10911103
return false;
10921104

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
7171
uint32 memory_offset = (uint32)LLVMConstIntGetZExtValue(moffset);
7272
uint32 init_page_count = comp_ctx->comp_data->mem_init_page_count;
7373
if (init_page_count > 0
74-
&& memory_offset <= NumBytesPerPage * init_page_count - bytes) {
74+
&& memory_offset <= comp_ctx->comp_data->num_bytes_per_page
75+
* init_page_count - bytes) {
7576
/* inside memory space */
7677
if (!func_ctx->mem_space_unchanged) {
7778
if (!(mem_base_addr = LLVMBuildLoad(comp_ctx->builder,

core/iwasm/interpreter/wasm.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ extern "C" {
2626
/* Table Element Type */
2727
#define TABLE_ELEM_TYPE_ANY_FUNC 0x70
2828

29-
#define MaxMemoryPages 65536
30-
#define MaxTableElems UINT32_MAX
31-
#define NumBytesPerPage 65536
32-
#define NumBytesPerPageLog2 16
33-
#define MaxReturnValues 16
29+
#define DEFAULT_NUM_BYTES_PER_PAGE 65536
3430

3531
#define INIT_EXPR_TYPE_I32_CONST 0x41
3632
#define INIT_EXPR_TYPE_I64_CONST 0x42
@@ -110,7 +106,7 @@ typedef struct WASMTable {
110106

111107
typedef struct WASMMemory {
112108
uint32 flags;
113-
/* 64 kbytes one page by default */
109+
uint32 num_bytes_per_page;
114110
uint32 init_page_count;
115111
uint32 max_page_count;
116112
} WASMMemory;
@@ -129,7 +125,7 @@ typedef struct WASMMemoryImport {
129125
char *module_name;
130126
char *field_name;
131127
uint32 flags;
132-
/* 64 kbytes one page by default */
128+
uint32 num_bytes_per_page;
133129
uint32 init_page_count;
134130
uint32 max_page_count;
135131
} WASMMemoryImport;
@@ -269,6 +265,16 @@ typedef struct WASMModule {
269265
WASMDataSeg **data_segments;
270266
uint32 start_function;
271267

268+
/* __data_end global exported by llvm */
269+
uint32 llvm_aux_data_end;
270+
/* auxiliary stack bottom, or __heap_base global exported by llvm */
271+
uint32 llvm_aux_stack_bottom;
272+
/* auxiliary stack size */
273+
uint32 llvm_aux_stack_size;
274+
/* the index of a global exported by llvm, which is
275+
auxiliary stack top pointer */
276+
uint32 llvm_aux_stack_global_index;
277+
272278
/* Whether there is possible memory grow, e.g.
273279
memory.grow opcode or call enlargeMemory */
274280
bool possible_memory_grow;

0 commit comments

Comments
 (0)