Skip to content

Commit 867dbd8

Browse files
authored
aot compiler: Propagate const-ness by ourselves (bytecodealliance#3567)
aot_load_const_from_table() hides the const-ness of the value and prevents optimizations like bytecodealliance#3552. This commit makes the aot compiler tracks the const-ness of the value directly in the AOTValue and enables the above mentioned optimization for XIP.
1 parent e66b414 commit 867dbd8

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

core/iwasm/compilation/aot_compiler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type)
605605
#define PUSH_PAGE_COUNT(v) \
606606
PUSH(v, MEMORY64_COND_VALUE(VALUE_TYPE_I64, VALUE_TYPE_I32))
607607

608+
#define SET_CONST(v) \
609+
do { \
610+
AOTValue *aot_value = \
611+
func_ctx->block_stack.block_list_end->value_stack.value_list_end; \
612+
aot_value->is_const = true; \
613+
aot_value->const_value = (v); \
614+
} while (0)
615+
608616
#define TO_LLVM_TYPE(wasm_type) \
609617
wasm_type_to_llvm_type(comp_ctx, &comp_ctx->basic_types, wasm_type)
610618

core/iwasm/compilation/aot_emit_const.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ aot_compile_op_i32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
2828
}
2929

3030
PUSH_I32(value);
31+
SET_CONST((uint64)(uint32)i32_const);
3132
return true;
3233
fail:
3334
return false;
@@ -55,6 +56,7 @@ aot_compile_op_i64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
5556
}
5657

5758
PUSH_I64(value);
59+
SET_CONST((uint64)i64_const);
5860
return true;
5961
fail:
6062
return false;

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
107107
LLVMBasicBlockRef check_succ;
108108
AOTValue *aot_value_top;
109109
uint32 local_idx_of_aot_value = 0;
110+
uint64 const_value;
110111
bool is_target_64bit, is_local_of_aot_value = false;
112+
bool is_const = false;
111113
#if WASM_ENABLE_SHARED_MEMORY != 0
112114
bool is_shared_memory =
113115
comp_ctx->comp_data->memories[0].flags & SHARED_MEMORY_FLAG;
@@ -162,7 +164,9 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
162164
/* aot_value_top is freed in the following POP_I32(addr),
163165
so save its fields here for further use */
164166
is_local_of_aot_value = aot_value_top->is_local;
167+
is_const = aot_value_top->is_const;
165168
local_idx_of_aot_value = aot_value_top->local_idx;
169+
const_value = aot_value_top->const_value;
166170
}
167171

168172
POP_MEM_OFFSET(addr);
@@ -172,9 +176,15 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
172176
* have been thrown when converting float to integer before
173177
*/
174178
/* return address directly if constant offset and inside memory space */
175-
if (LLVMIsEfficientConstInt(addr)) {
176-
uint64 mem_offset =
177-
(uint64)LLVMConstIntGetZExtValue(addr) + (uint64)offset;
179+
if (LLVMIsEfficientConstInt(addr) || is_const) {
180+
uint64 value;
181+
if (LLVMIsEfficientConstInt(addr)) {
182+
value = (uint64)LLVMConstIntGetZExtValue(addr);
183+
}
184+
else {
185+
value = const_value;
186+
}
187+
uint64 mem_offset = value + (uint64)offset;
178188
uint32 num_bytes_per_page =
179189
comp_ctx->comp_data->memories[0].num_bytes_per_page;
180190
uint32 init_page_count =

core/iwasm/compilation/aot_llvm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ typedef struct AOTValue {
7575
struct AOTValue *next;
7676
struct AOTValue *prev;
7777
LLVMValueRef value;
78+
uint64 const_value; /* valid if is_const is true */
79+
uint32 local_idx;
7880
/* VALUE_TYPE_I32/I64/F32/F64/VOID */
7981
uint8 type;
8082
bool is_local;
81-
uint32 local_idx;
83+
bool is_const;
8284
} AOTValue;
8385

8486
/**

0 commit comments

Comments
 (0)