Skip to content

Commit e3dcf4f

Browse files
author
xiangjia.xj
committed
feature: better memory information restore mechanism
1 parent 3f268e5 commit e3dcf4f

File tree

4 files changed

+171
-150
lines changed

4 files changed

+171
-150
lines changed

core/iwasm/compilation/aot_compiler.c

+9
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ static bool
999999
aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
10001000
{
10011001
AOTFuncContext *func_ctx = comp_ctx->func_ctxes[func_index];
1002+
WASMModule *module = comp_ctx->comp_data->wasm_module;
10021003
LLVMValueRef func_index_ref;
10031004
uint8 *frame_ip = func_ctx->aot_func->code, opcode, *p_f32, *p_f64;
10041005
uint8 *frame_ip_end = frame_ip + func_ctx->aot_func->code_size;
@@ -1230,6 +1231,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
12301231
read_leb_uint32(frame_ip, frame_ip_end, func_idx);
12311232
if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
12321233
return false;
1234+
if (module->functions[func_index]->has_memory_operations)
1235+
restore_memory_info(comp_ctx, func_ctx);
12331236
break;
12341237
}
12351238

@@ -1250,6 +1253,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
12501253
if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
12511254
tbl_idx))
12521255
return false;
1256+
if (module->functions[func_index]->has_memory_operations)
1257+
restore_memory_info(comp_ctx, func_ctx);
12531258
break;
12541259
}
12551260

@@ -1420,6 +1425,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
14201425
if (!aot_compile_op_call_ref(comp_ctx, func_ctx, type_idx,
14211426
false))
14221427
return false;
1428+
if (module->functions[func_index]->has_memory_operations)
1429+
restore_memory_info(comp_ctx, func_ctx);
14231430
break;
14241431
}
14251432

@@ -2092,6 +2099,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
20922099
read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
20932100
if (!aot_compile_op_memory_grow(comp_ctx, func_ctx))
20942101
return false;
2102+
if (module->functions[func_index]->has_memory_operations)
2103+
restore_memory_info(comp_ctx, func_ctx);
20952104
break;
20962105

20972106
case WASM_OP_I32_CONST:

core/iwasm/compilation/aot_emit_memory.c

+20-37
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ get_memory_check_bound(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
7878
return NULL;
7979
}
8080

81-
if (func_ctx->mem_space_unchanged)
82-
return mem_check_bound;
83-
8481
if (!(mem_check_bound = LLVMBuildLoad2(
8582
comp_ctx->builder,
8683
(comp_ctx->pointer_size == sizeof(uint64)) ? I64_TYPE : I32_TYPE,
@@ -164,17 +161,15 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
164161
}
165162

166163
/* Get memory base address and memory data size */
167-
if (func_ctx->mem_space_unchanged
168164
#if WASM_ENABLE_SHARED_MEMORY != 0
169-
|| is_shared_memory
170-
#endif
171-
) {
165+
if (is_shared_memory)
172166
mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
173-
}
174-
else {
167+
else
168+
#endif
169+
{
175170
if (!(mem_base_addr = LLVMBuildLoad2(
176171
comp_ctx->builder, OPQ_PTR_TYPE,
177-
func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
172+
func_ctx->mem_info[0].mem_base_addr, "mem_base_addr"))) {
178173
aot_set_last_error("llvm build load failed.");
179174
goto fail;
180175
}
@@ -1015,16 +1010,11 @@ get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
10151010
{
10161011
LLVMValueRef mem_size;
10171012

1018-
if (func_ctx->mem_space_unchanged) {
1019-
mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
1020-
}
1021-
else {
1022-
if (!(mem_size = LLVMBuildLoad2(
1023-
comp_ctx->builder, I32_TYPE,
1024-
func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
1025-
aot_set_last_error("llvm build load failed.");
1026-
goto fail;
1027-
}
1013+
if (!(mem_size = LLVMBuildLoad2(
1014+
comp_ctx->builder, I32_TYPE,
1015+
func_ctx->mem_info[0].mem_cur_page_count, "mem_size"))) {
1016+
aot_set_last_error("llvm build load failed.");
1017+
goto fail;
10281018
}
10291019

10301020
return LLVMBuildIntCast(comp_ctx->builder, mem_size,
@@ -1165,16 +1155,14 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
11651155
#if WASM_ENABLE_SHARED_MEMORY != 0
11661156
bool is_shared_memory = comp_ctx->comp_data->memories[0].flags & 0x02;
11671157

1168-
if (func_ctx->mem_space_unchanged || is_shared_memory) {
1169-
#else
1170-
if (func_ctx->mem_space_unchanged) {
1171-
#endif
1158+
if (is_shared_memory)
11721159
mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
1173-
}
1174-
else {
1160+
else
1161+
#endif
1162+
{
11751163
if (!(mem_base_addr = LLVMBuildLoad2(
11761164
comp_ctx->builder, OPQ_PTR_TYPE,
1177-
func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
1165+
func_ctx->mem_info[0].mem_base_addr, "mem_base_addr"))) {
11781166
aot_set_last_error("llvm build load failed.");
11791167
goto fail;
11801168
}
@@ -1206,16 +1194,11 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
12061194
}
12071195
}
12081196

1209-
if (func_ctx->mem_space_unchanged) {
1210-
mem_size = func_ctx->mem_info[0].mem_data_size_addr;
1211-
}
1212-
else {
1213-
if (!(mem_size = LLVMBuildLoad2(
1214-
comp_ctx->builder, I64_TYPE,
1215-
func_ctx->mem_info[0].mem_data_size_addr, "mem_size"))) {
1216-
aot_set_last_error("llvm build load failed.");
1217-
goto fail;
1218-
}
1197+
if (!(mem_size = LLVMBuildLoad2(
1198+
comp_ctx->builder, I64_TYPE,
1199+
func_ctx->mem_info[0].mem_data_size, "mem_size"))) {
1200+
aot_set_last_error("llvm build load failed.");
1201+
goto fail;
12191202
}
12201203

12211204
ADD_BASIC_BLOCK(check_succ, "check_succ");

0 commit comments

Comments
 (0)