From ff132c3c5d37f772fa6ef3b2566967c173ecd9b9 Mon Sep 17 00:00:00 2001 From: Zhenwei Jin <109658203+kylo5aby@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:22:15 +0800 Subject: [PATCH] add a toggle to enable extended const on wamrc (#4412) --- core/iwasm/compilation/aot_emit_aot_file.c | 16 +++++++++------- core/iwasm/compilation/aot_llvm.c | 3 +++ core/iwasm/compilation/aot_llvm.h | 3 +++ core/iwasm/include/aot_comp_option.h | 1 + doc/build_wamr.md | 4 ++++ .../wamr-test-suites/spec-test-script/runtest.py | 3 +++ wamr-compiler/CMakeLists.txt | 7 +------ wamr-compiler/main.c | 4 ++++ 8 files changed, 28 insertions(+), 13 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 31ca39402..208498fcb 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -1960,13 +1960,15 @@ aot_emit_init_expr(uint8 *buf, uint8 *buf_end, uint32 *p_offset, case INIT_EXPR_TYPE_I64_ADD: case INIT_EXPR_TYPE_I64_SUB: case INIT_EXPR_TYPE_I64_MUL: - if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx, - expr->u.binary.l_expr)) { - return false; - } - if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx, - expr->u.binary.r_expr)) { - return false; + if (comp_ctx->enable_extended_const) { + if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx, + expr->u.binary.l_expr)) { + return false; + } + if (!aot_emit_init_expr(buf, buf_end, &offset, comp_ctx, + expr->u.binary.r_expr)) { + return false; + } } break; #endif diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index c1708e3f9..4f6878148 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -2703,6 +2703,9 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) if (option->enable_shared_heap) comp_ctx->enable_shared_heap = true; + if (option->enable_extended_const) + comp_ctx->enable_extended_const = true; + comp_ctx->opt_level = option->opt_level; comp_ctx->size_level = option->size_level; diff --git a/core/iwasm/compilation/aot_llvm.h b/core/iwasm/compilation/aot_llvm.h index 6b1233c39..40c19da04 100644 --- a/core/iwasm/compilation/aot_llvm.h +++ b/core/iwasm/compilation/aot_llvm.h @@ -457,6 +457,9 @@ typedef struct AOTCompContext { /* Enable LLVM PGO (Profile-Guided Optimization) */ bool enable_llvm_pgo; + /* Enable extended constant expression */ + bool enable_extended_const; + /* Treat unknown import function as wasm-c-api import function and allow to directly invoke it from AOT/JIT code */ bool quick_invoke_c_api_import; diff --git a/core/iwasm/include/aot_comp_option.h b/core/iwasm/include/aot_comp_option.h index 839176623..836ff8bc3 100644 --- a/core/iwasm/include/aot_comp_option.h +++ b/core/iwasm/include/aot_comp_option.h @@ -68,6 +68,7 @@ typedef struct AOTCompOption { bool enable_ref_types; bool enable_gc; bool enable_aux_stack_check; + bool enable_extended_const; AOTStackFrameType aux_stack_frame_type; AOTCallStackFeatures call_stack_features; bool enable_perf_profiling; diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 94dd96284..d8c64eb50 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -292,6 +292,10 @@ Currently we only profile the memory consumption of module, module_instance and - **WAMR_BUILD_AOT_INTRINSICS**=1/0, enable the AOT intrinsic functions, default to enable if not set. These functions can be called from the AOT code when `--disable-llvm-intrinsics` flag or `--enable-builtin-intrinsics=` flag is used by wamrc to generate the AOT file. > Note: See [Tuning the XIP intrinsic functions](./xip.md#tuning-the-xip-intrinsic-functions) for more details. +### **Enable extended constant expression** +- **WAMR_BUILD_EXTENDED_CONST_EXPR**=1/0, default to disable if not set. +> Note: See [Extended Constant Expressions](https://github.com/WebAssembly/extended-const/blob/main/proposals/extended-const/Overview.md) for more details. + ### **Configurable memory access boundary check** - **WAMR_CONFIGURABLE_BOUNDS_CHECKS**=1/0, default to disable if not set > Note: If it is enabled, allow to run `iwasm --disable-bounds-checks` to disable the memory access boundary checks for interpreter mode. diff --git a/tests/wamr-test-suites/spec-test-script/runtest.py b/tests/wamr-test-suites/spec-test-script/runtest.py index e0d37b969..fa9f5eb7d 100755 --- a/tests/wamr-test-suites/spec-test-script/runtest.py +++ b/tests/wamr-test-suites/spec-test-script/runtest.py @@ -1160,6 +1160,9 @@ def compile_wasm_to_aot(wasm_tempfile, aot_tempfile, runner, opts, r, output = ' cmd.append("--enable-gc") cmd.append("--enable-tail-call") + if opts.extended_const: + cmd.append("--enable-extended-const") + if output == 'object': cmd.append("--format=object") elif output == 'ir': diff --git a/wamr-compiler/CMakeLists.txt b/wamr-compiler/CMakeLists.txt index c92e2ae22..8f4c7ff16 100644 --- a/wamr-compiler/CMakeLists.txt +++ b/wamr-compiler/CMakeLists.txt @@ -38,12 +38,6 @@ else() add_definitions(-DWASM_ENABLE_SIMD=1) endif() -if (WAMR_BUILD_EXTENDED_CONST_EXPR EQUAL 0) - add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=0) -else() - add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=1) -endif() - add_definitions(-DWASM_ENABLE_INTERP=1) add_definitions(-DWASM_ENABLE_WAMR_COMPILER=1) add_definitions(-DWASM_ENABLE_BULK_MEMORY=1) @@ -59,6 +53,7 @@ add_definitions(-DWASM_ENABLE_PERF_PROFILING=1) add_definitions(-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1) add_definitions(-DWASM_ENABLE_MODULE_INST_CONTEXT=1) add_definitions(-DWASM_ENABLE_MEMORY64=1) +add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=1) add_definitions(-DWASM_ENABLE_GC=1) diff --git a/wamr-compiler/main.c b/wamr-compiler/main.c index f74b2fb15..3c1c44ff7 100644 --- a/wamr-compiler/main.c +++ b/wamr-compiler/main.c @@ -418,6 +418,7 @@ main(int argc, char *argv[]) option.enable_bulk_memory = true; option.enable_ref_types = true; option.enable_gc = false; + option.enable_extended_const = false; aot_call_stack_features_init_default(&option.call_stack_features); /* Process options */ @@ -531,6 +532,9 @@ main(int argc, char *argv[]) else if (!strcmp(argv[0], "--disable-aux-stack-check")) { option.enable_aux_stack_check = false; } + else if (!strcmp(argv[0], "--enable-extended-const")) { + option.enable_extended_const = true; + } else if (!strcmp(argv[0], "--enable-dump-call-stack")) { option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD; }