From 4c127715df2abd65a16b685e2fe98681e85e403b Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 16 Aug 2024 14:04:41 +0800 Subject: [PATCH] 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. --- core/iwasm/compilation/aot_emit_const.c | 60 ++++++++++++------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_const.c b/core/iwasm/compilation/aot_emit_const.c index 5665b480..64fa3ded 100644 --- a/core/iwasm/compilation/aot_emit_const.c +++ b/core/iwasm/compilation/aot_emit_const.c @@ -68,23 +68,21 @@ aot_compile_op_f32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, { LLVMValueRef alloca, value; - if (!isnan(f32_const)) { - if (comp_ctx->is_indirect_mode - && aot_intrinsic_check_capability(comp_ctx, "f32.const")) { - WASMValue wasm_value; - memcpy(&wasm_value.f32, &f32_const, sizeof(float32)); - value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol, - &wasm_value, VALUE_TYPE_F32); - if (!value) { - return false; - } - PUSH_F32(value); - } - else { - value = F32_CONST(f32_const); - CHECK_LLVM_CONST(value); - PUSH_F32(value); + if (comp_ctx->is_indirect_mode + && aot_intrinsic_check_capability(comp_ctx, "f32.const")) { + WASMValue wasm_value; + memcpy(&wasm_value.f32, &f32_const, sizeof(float32)); + value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol, + &wasm_value, VALUE_TYPE_F32); + if (!value) { + return false; } + PUSH_F32(value); + } + else if (!isnan(f32_const)) { + value = F32_CONST(f32_const); + CHECK_LLVM_CONST(value); + PUSH_F32(value); } else { int32 i32_const; @@ -123,23 +121,21 @@ aot_compile_op_f64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, { LLVMValueRef alloca, value; - if (!isnan(f64_const)) { - if (comp_ctx->is_indirect_mode - && aot_intrinsic_check_capability(comp_ctx, "f64.const")) { - WASMValue wasm_value; - memcpy(&wasm_value.f64, &f64_const, sizeof(float64)); - value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol, - &wasm_value, VALUE_TYPE_F64); - if (!value) { - return false; - } - PUSH_F64(value); - } - else { - value = F64_CONST(f64_const); - CHECK_LLVM_CONST(value); - PUSH_F64(value); + if (comp_ctx->is_indirect_mode + && aot_intrinsic_check_capability(comp_ctx, "f64.const")) { + WASMValue wasm_value; + memcpy(&wasm_value.f64, &f64_const, sizeof(float64)); + value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol, + &wasm_value, VALUE_TYPE_F64); + if (!value) { + return false; } + PUSH_F64(value); + } + else if (!isnan(f64_const)) { + value = F64_CONST(f64_const); + CHECK_LLVM_CONST(value); + PUSH_F64(value); } else { int64 i64_const;