Skip to content

Commit 5be427b

Browse files
authored
xip: Lookup float constants from table to reduce relocations (#894)
Lookup float/double constants from exec_env->native_symbol table but not construct them with LLVMBuildConst if XIP mode is enabled, these constants are introduced by f32/f64.const opcodes and some float/double conversion opcodes, and make wamrc generate some relocations in text section of AOT XIP file. This patch eliminates such relocations when "--enable-indirect-mode" is added to wamrc.
1 parent f2d87ec commit 5be427b

File tree

6 files changed

+287
-45
lines changed

6 files changed

+287
-45
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ get_native_symbol_by_name(const char *name)
465465
return func;
466466
}
467467

468+
static bool
469+
str2uint32(const char *buf, uint32 *p_res);
470+
471+
static bool
472+
str2uint64(const char *buf, uint64 *p_res);
473+
468474
static bool
469475
load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
470476
AOTModule *module, bool is_load_from_file_buf,
@@ -487,11 +493,39 @@ load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
487493

488494
for (i = cnt - 1; i >= 0; i--) {
489495
read_string(p, p_end, symbol);
490-
module->native_symbol_list[i] = get_native_symbol_by_name(symbol);
491-
if (module->native_symbol_list[i] == NULL) {
492-
set_error_buf_v(error_buf, error_buf_size,
493-
"missing native symbol: %s", symbol);
494-
goto fail;
496+
if (!strncmp(symbol, "f32#", 4)) {
497+
uint32 u32;
498+
/* Resolve the raw int bits of f32 const */
499+
if (!str2uint32(symbol + 4, &u32)) {
500+
set_error_buf_v(error_buf, error_buf_size,
501+
"resolve symbol %s failed", symbol);
502+
goto fail;
503+
}
504+
*(uint32 *)(&module->native_symbol_list[i]) = u32;
505+
}
506+
else if (!strncmp(symbol, "f64#", 4)) {
507+
uint64 u64;
508+
/* Resolve the raw int bits of f64 const */
509+
if (!str2uint64(symbol + 4, &u64)) {
510+
set_error_buf_v(error_buf, error_buf_size,
511+
"resolve symbol %s failed", symbol);
512+
goto fail;
513+
}
514+
*(uint64 *)(&module->native_symbol_list[i]) = u64;
515+
}
516+
else if (!strncmp(symbol, "__ignore", 8)) {
517+
/* Padding bytes to make f64 on 8-byte aligned address,
518+
or it is the second 32-bit slot in 32-bit system */
519+
continue;
520+
}
521+
else {
522+
module->native_symbol_list[i] =
523+
get_native_symbol_by_name(symbol);
524+
if (module->native_symbol_list[i] == NULL) {
525+
set_error_buf_v(error_buf, error_buf_size,
526+
"missing native symbol: %s", symbol);
527+
goto fail;
528+
}
495529
}
496530
}
497531
}
@@ -1711,7 +1745,6 @@ is_literal_relocation(const char *reloc_sec_name)
17111745
return !strcmp(reloc_sec_name, ".rela.literal");
17121746
}
17131747

1714-
#if defined(BH_PLATFORM_WINDOWS)
17151748
static bool
17161749
str2uint32(const char *buf, uint32 *p_res)
17171750
{
@@ -1757,7 +1790,6 @@ str2uint64(const char *buf, uint64 *p_res)
17571790
*p_res = res;
17581791
return true;
17591792
}
1760-
#endif
17611793

17621794
static bool
17631795
do_text_relocation(AOTModule *module, AOTRelocationGroup *group,

core/iwasm/compilation/aot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ typedef struct AOTCompData {
268268

269269
typedef struct AOTNativeSymbol {
270270
bh_list_link link;
271-
const char *symbol;
271+
char symbol[32];
272272
int32 index;
273273
} AOTNativeSymbol;
274274

core/iwasm/compilation/aot_emit_const.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,21 @@ aot_compile_op_f32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
3636
LLVMValueRef alloca, value;
3737

3838
if (!isnan(f32_const)) {
39-
value = F32_CONST(f32_const);
40-
CHECK_LLVM_CONST(value);
41-
PUSH_F32(value);
39+
if (!comp_ctx->is_indirect_mode) {
40+
value = F32_CONST(f32_const);
41+
CHECK_LLVM_CONST(value);
42+
PUSH_F32(value);
43+
}
44+
else {
45+
WASMValue wasm_value;
46+
memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
47+
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
48+
&wasm_value, VALUE_TYPE_F32);
49+
if (!value) {
50+
return false;
51+
}
52+
PUSH_F32(value);
53+
}
4254
}
4355
else {
4456
int32 i32_const;
@@ -77,9 +89,21 @@ aot_compile_op_f64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
7789
LLVMValueRef alloca, value;
7890

7991
if (!isnan(f64_const)) {
80-
value = F64_CONST(f64_const);
81-
CHECK_LLVM_CONST(value);
82-
PUSH_F64(value);
92+
if (!comp_ctx->is_indirect_mode) {
93+
value = F64_CONST(f64_const);
94+
CHECK_LLVM_CONST(value);
95+
PUSH_F64(value);
96+
}
97+
else {
98+
WASMValue wasm_value;
99+
memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
100+
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
101+
&wasm_value, VALUE_TYPE_F64);
102+
if (!value) {
103+
return false;
104+
}
105+
PUSH_F64(value);
106+
}
83107
}
84108
else {
85109
int64 i64_const;

core/iwasm/compilation/aot_emit_conversion.c

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,37 @@ aot_compile_op_i32_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
348348

349349
POP_F32(value);
350350

351-
if (sign) {
352-
min_value = F32_CONST(-2147483904.0f);
353-
max_value = F32_CONST(2147483648.0f);
351+
if (!comp_ctx->is_indirect_mode) {
352+
if (sign) {
353+
min_value = F32_CONST(-2147483904.0f);
354+
max_value = F32_CONST(2147483648.0f);
355+
}
356+
else {
357+
min_value = F32_CONST(-1.0f);
358+
max_value = F32_CONST(4294967296.0f);
359+
}
354360
}
355361
else {
356-
min_value = F32_CONST(-1.0f);
357-
max_value = F32_CONST(4294967296.0f);
362+
WASMValue wasm_value;
363+
if (sign) {
364+
wasm_value.f32 = -2147483904.0f;
365+
min_value = aot_load_const_from_table(
366+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
367+
wasm_value.f32 = 2147483648.0f;
368+
max_value = aot_load_const_from_table(
369+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
370+
}
371+
else {
372+
wasm_value.f32 = -1.0f;
373+
min_value = aot_load_const_from_table(
374+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
375+
wasm_value.f32 = 4294967296.0f;
376+
max_value = aot_load_const_from_table(
377+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
378+
}
358379
}
380+
CHECK_LLVM_CONST(min_value);
381+
CHECK_LLVM_CONST(max_value);
359382

360383
if (!saturating)
361384
return trunc_float_to_int(
@@ -378,14 +401,37 @@ aot_compile_op_i32_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
378401

379402
POP_F64(value);
380403

381-
if (sign) {
382-
min_value = F64_CONST(-2147483649.0);
383-
max_value = F64_CONST(2147483648.0);
404+
if (!comp_ctx->is_indirect_mode) {
405+
if (sign) {
406+
min_value = F64_CONST(-2147483649.0);
407+
max_value = F64_CONST(2147483648.0);
408+
}
409+
else {
410+
min_value = F64_CONST(-1.0);
411+
max_value = F64_CONST(4294967296.0);
412+
}
384413
}
385414
else {
386-
min_value = F64_CONST(-1.0);
387-
max_value = F64_CONST(4294967296.0);
415+
WASMValue wasm_value;
416+
if (sign) {
417+
wasm_value.f64 = -2147483649.0;
418+
min_value = aot_load_const_from_table(
419+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
420+
wasm_value.f64 = 2147483648.0;
421+
max_value = aot_load_const_from_table(
422+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
423+
}
424+
else {
425+
wasm_value.f64 = -1.0;
426+
min_value = aot_load_const_from_table(
427+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
428+
wasm_value.f64 = 4294967296.0;
429+
max_value = aot_load_const_from_table(
430+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
431+
}
388432
}
433+
CHECK_LLVM_CONST(min_value);
434+
CHECK_LLVM_CONST(max_value);
389435

390436
if (!saturating)
391437
return trunc_float_to_int(
@@ -509,14 +555,37 @@ aot_compile_op_i64_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
509555

510556
POP_F32(value);
511557

512-
if (sign) {
513-
min_value = F32_CONST(-9223373136366403584.0f);
514-
max_value = F32_CONST(9223372036854775808.0f);
558+
if (!comp_ctx->is_indirect_mode) {
559+
if (sign) {
560+
min_value = F32_CONST(-9223373136366403584.0f);
561+
max_value = F32_CONST(9223372036854775808.0f);
562+
}
563+
else {
564+
min_value = F32_CONST(-1.0f);
565+
max_value = F32_CONST(18446744073709551616.0f);
566+
}
515567
}
516568
else {
517-
min_value = F32_CONST(-1.0f);
518-
max_value = F32_CONST(18446744073709551616.0f);
569+
WASMValue wasm_value;
570+
if (sign) {
571+
wasm_value.f32 = -9223373136366403584.0f;
572+
min_value = aot_load_const_from_table(
573+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
574+
wasm_value.f32 = 9223372036854775808.0f;
575+
max_value = aot_load_const_from_table(
576+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
577+
}
578+
else {
579+
wasm_value.f32 = -1.0f;
580+
min_value = aot_load_const_from_table(
581+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
582+
wasm_value.f32 = 18446744073709551616.0f;
583+
max_value = aot_load_const_from_table(
584+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
585+
}
519586
}
587+
CHECK_LLVM_CONST(min_value);
588+
CHECK_LLVM_CONST(max_value);
520589

521590
if (!saturating)
522591
return trunc_float_to_int(
@@ -539,14 +608,37 @@ aot_compile_op_i64_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
539608

540609
POP_F64(value);
541610

542-
if (sign) {
543-
min_value = F64_CONST(-9223372036854777856.0);
544-
max_value = F64_CONST(9223372036854775808.0);
611+
if (!comp_ctx->is_indirect_mode) {
612+
if (sign) {
613+
min_value = F64_CONST(-9223372036854777856.0);
614+
max_value = F64_CONST(9223372036854775808.0);
615+
}
616+
else {
617+
min_value = F64_CONST(-1.0);
618+
max_value = F64_CONST(18446744073709551616.0);
619+
}
545620
}
546621
else {
547-
min_value = F64_CONST(-1.0);
548-
max_value = F64_CONST(18446744073709551616.0);
622+
WASMValue wasm_value;
623+
if (sign) {
624+
wasm_value.f64 = -9223372036854777856.0;
625+
min_value = aot_load_const_from_table(
626+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
627+
wasm_value.f64 = 9223372036854775808.0;
628+
max_value = aot_load_const_from_table(
629+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
630+
}
631+
else {
632+
wasm_value.f64 = -1.0;
633+
min_value = aot_load_const_from_table(
634+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
635+
wasm_value.f64 = 18446744073709551616.0;
636+
max_value = aot_load_const_from_table(
637+
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
638+
}
549639
}
640+
CHECK_LLVM_CONST(min_value);
641+
CHECK_LLVM_CONST(max_value);
550642

551643
if (!saturating)
552644
return trunc_float_to_int(

0 commit comments

Comments
 (0)