Skip to content

Commit 4c12771

Browse files
authored
aot compiler: Fix NaN handling for opcode f32/f64.const in XIP mode (#3721)
If the value of a float constant is an NaN, the aot compiler creates an alloca, stores the converted i32 const into it and then loads f32 from it again, which may introduce a relocation in the AOT file and is not allowed for XIP mode.
1 parent 59f761b commit 4c12771

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

core/iwasm/compilation/aot_emit_const.c

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,21 @@ aot_compile_op_f32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
6868
{
6969
LLVMValueRef alloca, value;
7070

71-
if (!isnan(f32_const)) {
72-
if (comp_ctx->is_indirect_mode
73-
&& aot_intrinsic_check_capability(comp_ctx, "f32.const")) {
74-
WASMValue wasm_value;
75-
memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
76-
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
77-
&wasm_value, VALUE_TYPE_F32);
78-
if (!value) {
79-
return false;
80-
}
81-
PUSH_F32(value);
82-
}
83-
else {
84-
value = F32_CONST(f32_const);
85-
CHECK_LLVM_CONST(value);
86-
PUSH_F32(value);
71+
if (comp_ctx->is_indirect_mode
72+
&& aot_intrinsic_check_capability(comp_ctx, "f32.const")) {
73+
WASMValue wasm_value;
74+
memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
75+
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
76+
&wasm_value, VALUE_TYPE_F32);
77+
if (!value) {
78+
return false;
8779
}
80+
PUSH_F32(value);
81+
}
82+
else if (!isnan(f32_const)) {
83+
value = F32_CONST(f32_const);
84+
CHECK_LLVM_CONST(value);
85+
PUSH_F32(value);
8886
}
8987
else {
9088
int32 i32_const;
@@ -123,23 +121,21 @@ aot_compile_op_f64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
123121
{
124122
LLVMValueRef alloca, value;
125123

126-
if (!isnan(f64_const)) {
127-
if (comp_ctx->is_indirect_mode
128-
&& aot_intrinsic_check_capability(comp_ctx, "f64.const")) {
129-
WASMValue wasm_value;
130-
memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
131-
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
132-
&wasm_value, VALUE_TYPE_F64);
133-
if (!value) {
134-
return false;
135-
}
136-
PUSH_F64(value);
137-
}
138-
else {
139-
value = F64_CONST(f64_const);
140-
CHECK_LLVM_CONST(value);
141-
PUSH_F64(value);
124+
if (comp_ctx->is_indirect_mode
125+
&& aot_intrinsic_check_capability(comp_ctx, "f64.const")) {
126+
WASMValue wasm_value;
127+
memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
128+
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
129+
&wasm_value, VALUE_TYPE_F64);
130+
if (!value) {
131+
return false;
142132
}
133+
PUSH_F64(value);
134+
}
135+
else if (!isnan(f64_const)) {
136+
value = F64_CONST(f64_const);
137+
CHECK_LLVM_CONST(value);
138+
PUSH_F64(value);
143139
}
144140
else {
145141
int64 i64_const;

0 commit comments

Comments
 (0)