wasm-micro-runtime/wamr-compiler/main.c
Zhenwei Jin 419f5cbf9e
Merge and solve conflict for extended const (#4435)
* build(deps): Bump github/codeql-action from 3.28.18 to 3.28.19 (#4346)

Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.18 to 3.28.19.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Commits](https://github.com/github/codeql-action/compare/v3.28.18...v3.28.19)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.19
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* wasi_socket_ext.c: avoid tls to make this library-friendly (#4338)

* Enable aot memory64 sw bounds checks by default (#4350)

- enable aot memory64 sw bounds checks by default

* build(deps): Bump requests from 2.32.3 to 2.32.4 in /build-scripts (#4349)

Bumps [requests](https://github.com/psf/requests) from 2.32.3 to 2.32.4.
- [Release notes](https://github.com/psf/requests/releases)
- [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md)
- [Commits](https://github.com/psf/requests/compare/v2.32.3...v2.32.4)

---
updated-dependencies:
- dependency-name: requests
  dependency-version: 2.32.4
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* wasi_nn_types.h: remove a seemingly stale comment (#4348)

* add heap-type check for GC when ref.null (#4300)

- According to [Link 1](https://webassembly.github.io/gc/core/valid/instructions.html#xref-syntax-instructions-syntax-instr-ref-mathsf-ref-null-mathit-ht), we must ensure that the heap type is valid when ref.null.
- According to [Link 2](https://webassembly.github.io/gc/core/valid/types.html#heap-types), a heap type is considered valid if it is either a concrete heap type or an abstract heap type.

However, in this function, the check for abstract heap types (absheaptype) was clearly missing, so this condition needs to be added explicitly in the if statement.

- When GC is disabled, no change is needed.
- When GC is enabled, heap types in WAMR are LEB-encoded values ([Link 3](https://webassembly.github.io/gc/core/appendix/index-types.html)). Therefore, we must use read_leb_int32 to parse the heap type correctly. And we can compute the original type1 using type1 = (uint8)((int32)0x80 + heap_type);.

* wamr-wasi-extensions: add a cmake package to provide our wasi extension (#4344)

* wasi_ephemeral_nn.h: add a convenience wrapper header
* wamr-wasi-extensions: add a cmake package to provide our wasi extension

the sample app was tested with:
* wasmtime
* iwasm with https://github.com/bytecodealliance/wasm-micro-runtime/pull/4308

currently only contains wasi-nn.
maybe it makes sense to add lib-socket things as well.

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4288

* wasi_nn_openvino.c: remove the tensor layout adjustment logic (#4308)

the logic in question seems like an attempt to work around
some application bugs.
my wild guess is that it was for classification-example.
cf. https://github.com/bytecodealliance/wasmtime/issues/10867

* Update type validation in load_table_import() and load_table() (#4296)

Prevent from value type.

https://webassembly.github.io/spec/core/valid/types.html#table-types
https://webassembly.github.io/gc/core/syntax/types.html#reference-types

* Follow #4268 to deprecate wamr_ide-related components (#4341)

refer to: Bypass wamr_ide-related components from the release process. (#4268)

* clean up incompatible running mode checks in test script and ci (#4342)

Rearrange the content of do_execute_in_running_mode() in alphabetical
order. 

Add an incompatible check for x86_32. Now, all belows will be bypassed:
- jit, fast-jit, multi-tier-jit
- memory64
- multi-memory
- simd

* Update WABT downloads URL (#4357)

Plus, skip unsupported running mode instead quit during wamr compiler
test

* Modify AOT static PGO to conform to llvm-18 and add a CI job to test static PGO on the coremark benchmark (#4345)

* static PGO compatible with llvm18 and add CI job to test static PGO on coremark benchmark
* update comments and warning info, bitmaps section in llvm profdata shouldn't be used in PGO

* Collective fix for typos and minor bugs (#4369)

* wasi-nn: fix backend leak on multiple loads (#4366)

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4340

* build(deps): Bump github/codeql-action from 3.28.19 to 3.29.0 (#4371)

Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.19 to 3.29.0.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Commits](https://github.com/github/codeql-action/compare/v3.28.19...v3.29.0)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* add validation for array type in load_init_expr(GC only) (#4370)

* wasi_nn_openvino.c: remove broken xml check (#4365)

`xml.buf[xml.size]` check is broken because it accesses past
the end of the buffer.

anyway, openvino doesn't seem to care the NUL termination.

* wamr-wasi-extensions: add lib-socket things (#4360)

* improve installation steps for wasi-sdk and wabt on Windows (#4359)

* wasi_ephemeral_nn.h: prefix identfiers to avoid too generic names (#4358)

* wasi_nn_openvino.c: add a missing buffer overflow check in get_output (#4353)

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4351

* send an empty/error reply from server (#4362)

Signed-off-by: Su Yihan <yihan.su@intel.com>

* wasi_nn_openvino.c: remove pre/postprocessing and layout assumptions (#4361)

as wasi-nn doesn't have these concepts, the best we can do without
risking breaking certain applications here is to pass through tensors
as they are.

this matches wasmtime's behavior.

tested with:

* wasmtime classification-example
  (with this change, this example fails on tensor size mismatch
  instead of implicitly resizing it.)

* license-plate-recognition-barrier-0007, a converted version
  with non-fp32 output. [1]
  (with this change, this model outputs integers as expected.)

[1] cd7ebe313b/models/public/license-plate-recognition-barrier-0007

* add nn-cli example (#4373)

an example application with flexible cli options which
aims to allow us to perform any wasi-nn operations.

eg.
```
--load-graph=file=fixture/model.xml,file=fixture/model.bin,id=graph
--init-execution-context=graph-id=graph,id=ctx
--set-input=file=fixture/tensor.bgr,context-id=ctx,dim=1,dim=3,dim=224,dim=224
--compute=context-id=ctx
--get-output=context-id=ctx,file=output.bin
```

* wasi-nn: apply the shared library hack to darwin as well (#4374)

copied from the linux version.

i'm a bit skeptical with this workaround though.
it might be simpler to prohibit the use of wamr api in these
shared libraries. after all, what these libraries do is nothing
specific to wasm.

* wasi-nn: don't try to deinit uninitialized backend (#4375)

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4339

* core/iwasm/libraries/wasi-nn/test/build.sh: add a tip for intel mac (#4389)

i keep forgetting this and had to re-investigate it at least twice.
hopefully this can be helpful for others too.

* wasi_nn_tensorflowlite.cpp: reject non-fp32 input earlier (#4388)

this backend assumes fp32 here and there.
it's safer to reject unexpected inputs explicitly.

* Fix several issues related to night-run CI and test scripts. (#4385)

- remove duplicated options
- fix test script
- change ci to use binary

* core/iwasm/libraries/wasi-nn/test: use the correct version of keras (#4383)

* wasi-nn: fix tensor_data abi for wasi_ephemeral_nn (#4379)

it's "(list u8)" in the witx definition.

the new definition matches both of our own host definition
(struct tensor_wasm) and wasmtime.

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4352

* enable WAMR_BUILD_WASI_EPHEMERAL_NN by default (#4381)

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4326

* deprecate legacy WAMR-specific "wasi_nn" module (#4382)

wasi_nn.h: deprecate legacy "wasi_nn"

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4326

* wasi-nn: add minimum serialization on WASINNContext (#4387)

currently this is not necessary because context (WASINNContext) is
local to instance. (wasm_module_instance_t)

i plan to make a context shared among instances in a cluster when
fixing https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313.
this is a preparation for that direction.

an obvious alternative is to tweak the module instance context APIs
to allow declaring some kind of contexts instance-local. but i feel,
in this particular case, it's more natural to make "wasi-nn handles"
shared among threads within a "process".

note that, spec-wise, how wasi-nn behaves wrt threads is not defined
at all because wasi officially doesn't have threads yet. i suppose, at
this point, that how wasi-nn interacts with wasi-threads is something
we need to define by ourselves, especially when we are using an outdated
wasi-nn version.

with this change, if a thread attempts to access a context while
another thread is using it, we simply make the operation fail with
the "busy" error. this is intended for the mimimum serialization to
avoid problems like crashes/leaks/etc. this is not intended to allow
parallelism or such.

no functional changes are intended at this point yet.

cf.
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2430

* Improve spec test execution by adding retry logic for transient errors (#4393)

* wasi_nn_openvino.c: implement multiple models per instance (#4380)

tested with two models:
```
--load-graph=id=graph1,file=public/license-plate-recognition-barrier-0007/FP32/license-plate-recognition-barrier-0007.xml,file=public/license-plate-recognition-barrier-0007/FP32/license-plate-recognition-barrier-0007.bin \
--load-graph=id=graph2,file=classify/model.xml,file=classify/model.bin \
--init-execution-context=id=exec1,graph-id=graph1 \
--init-execution-context=id=exec2,graph-id=graph2 \
--set-input=context-id=exec1,dim=1,dim=24,dim=94,dim=3,file=out.bin \
--set-input=context-id=exec2,file=classify/banana-3x224x224-bgr.bin,dim=1,dim=3,dim=224,dim=224 \
--compute=context-id=exec1 \
--compute=context-id=exec2 \
--get-output=context-id=exec1,file=exec1-result.bin \
--get-output=context-id=exec2,file=exec2-result.bin
```

a detailed HOWTO: https://github.com/bytecodealliance/wasm-micro-runtime/pull/4380#issuecomment-2986882718

* wamr-wasi-extensions/socket: disable reference-types (#4392)

and add a comment to explain why.

* CI: fix the description of upload_url (#4407)

* wasi-nn: fix context lifetime issues (#4396)

* wasi-nn: fix context lifetime issues

use the module instance context api instead of trying to roll
our own with a hashmap. this fixes context lifetime problems mentioned in
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313.

namely,

* wasi-nn resources will be freed earlier now. before this change,
  they used to be kept until the runtime shutdown. (wasm_runtime_destroy)
  after this change, they will be freed together with the associated
  instances.

* wasm_module_inst_t pointer uniqueness assumption (which is wrong
  after wasm_runtime_deinstantiate) was lifted.

as a side effect, this change also makes a context shared among threads
within a cluster. note that this is a user-visible api/abi breaking change.
before this change, wasi-nn "handles" like wasi_ephemeral_nn_graph were
thread-local. after this change, they are shared among threads within
a cluster, similarly to wasi file descriptors. spec-wise, either behavior
should be ok simply because wasi officially doesn't have threads yet.
althogh i feel the latter semantics is more intuitive, if your application
depends on the thread-local behavior, this change breaks your application.

tested with wamr-wasi-extensions/samples/nn-cli, modified to
call each wasi-nn operations on different threads. (if you are
interested, you can find the modification at
https://github.com/yamt/wasm-micro-runtime/tree/yamt-nn-wip-20250619.)

cf.
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4313
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2430

* runtime_lib.cmake: enable WAMR_BUILD_MODULE_INST_CONTEXT for wasi-nn

as we do for wasi (WAMR_BUILD_LIBC_WASI)

* wasi_nn_tensorflowlite.cpp: fix get_output return size (#4390)

it should be byte size, not the number of (fp32) values.

i'm ambivalent about how to deal with the compatibility for
the legacy wamr-specific "wasi_nn". for now, i avoided changing it.
(so that existing tests using the legacy abi, namely test_tensorflow.c
and test_tensorflow_quantized.c, passes as they are.)
if we have any users who still want to use the legacy abi,
i suppose they consider the compatibility is more important
than the consistency with other backends.

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4376

* Refactor copy callstack feature (#4401)

- Change `WAMR_ENABLE_COPY_CALLSTACK` to `WAMR_BUILD_COPY_CALL_STACK`, as
  `WAMR_BUILD` is the prefix for a command line option.
- Change `WAMR_ENABLE_COPY_CALLSTACK` to `WASM_ENABLE_COPY_CALL_STACK`, as
  `WASM_ENABLE` is the prefix for a macro in the source code.
- Change `CALLSTACK` to `CALL_STACK` to align with the existing
  `DUMP_CALL_STACK` feature.
- Continue using `WASMCApiFrame` instead of `wasm_frame_t` outside of
  *wasm_c_api.xxx* to avoid a typedef redefinition warning, which is
  identified by Clang.

* loader: add type index checking (#4402)

* Fix handling of non-nullable global_type during global import (#4408)

* wasi_nn_llamacpp.c: make this compilable (#4403)

* fix bug in bh_vector when extending (#4414)

* Collective fix (#4413)

* Fix vector growth check and typos in core (#9)
* Fix resource cleanup in memory and running modes tests (#10)
* Add end of file empty line in wasm_running_modes_test.cc

* wasi-nn: make the host use the wasi_ephemeral_nn version of tensor_data (#4411)

the motivations:

* make the actual input size available to the backends.
  (currently the backends have to make a guess from shape/type.)

* make the host logic look a bit similar to wasi_ephemeral_nn.

this is a backend api/abi change.

* wasi_nn_llamacpp.c: fix buffer overruns in set_input (#4420)

note: for some reasons, wasmedge seems to ignore type/dimensions
for the input of ggml. some user code relies on it.
cf. https://github.com/second-state/WasmEdge-WASINN-examples/issues/196

note: despite the comment in our code, the input doesn't seem
nul-terminated.

* wasi_nn_llamacpp.c: remove an unused variable (#4415)

* Fix few shadow warnings (#4409)

- declaration of ‘memidx’ shadows a previous local
- declaration of ‘count’ shadows a previous local

* CI: build wamr-wasi-extensions (#4394)

* wamr-wasi-extensions: separate test scripts
also, allow to specify the prefix directory.
for the convenience of the CI.

* CI: build wamr-wasi-extensions
fragments are copied from compilation_on_macos.yml.
(thus intel copyright notice)

* wasi_nn_openvino.c: fix a debug build (#4416)

after "wasi_nn_openvino.c: implement multiple models per instance" change.
(https://github.com/bytecodealliance/wasm-micro-runtime/pull/4380)

* loader: fix a potential overflow issue (#4427)

* CI: revert SGX retry attempts (#4421)

* Revert "Improve spec test execution by adding retry logic for transient errors (#4393)"

This reverts commit 64cafaff1e.

* Revert "Add error handling for sgx ci (#4222)"

This reverts commit 8ad47897d1.

* implement extended const expr (#4318)

* add a toggle to enable extended const on wamrc (#4412)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Su Yihan <yihan.su@intel.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Co-authored-by: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com>
Co-authored-by: Liu Jia <jia3.liu@intel.com>
Co-authored-by: liang.he <liang.he@intel.com>
Co-authored-by: Su Yihan <yihan.su@intel.com>
2025-07-01 10:39:44 +08:00

885 lines
35 KiB
C

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdlib.h>
#include "bh_platform.h"
#include "bh_read_file.h"
#include "wasm_export.h"
#include "aot_export.h"
#include <llvm-c/Support.h>
#if BH_HAS_DLFCN
#include <dlfcn.h>
typedef uint32 (*get_native_lib_func)(char **p_module_name,
NativeSymbol **p_native_symbols);
static uint32
load_and_register_native_libs(const char **native_lib_list,
uint32 native_lib_count,
void **native_handle_list)
{
uint32 i, native_handle_count = 0, n_native_symbols;
NativeSymbol *native_symbols;
char *module_name;
void *handle;
for (i = 0; i < native_lib_count; i++) {
/* open the native library */
if (!(handle = dlopen(native_lib_list[i], RTLD_NOW | RTLD_GLOBAL))
&& !(handle = dlopen(native_lib_list[i], RTLD_LAZY))) {
LOG_WARNING("warning: failed to load native library %s",
native_lib_list[i]);
continue;
}
/* lookup get_native_lib func */
get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
if (!get_native_lib) {
LOG_WARNING("warning: failed to lookup `get_native_lib` function "
"from native lib %s",
native_lib_list[i]);
dlclose(handle);
continue;
}
n_native_symbols = get_native_lib(&module_name, &native_symbols);
/* register native symbols */
if (!(n_native_symbols > 0 && module_name && native_symbols
&& wasm_runtime_register_natives(module_name, native_symbols,
n_native_symbols))) {
LOG_WARNING("warning: failed to register native lib %s",
native_lib_list[i]);
dlclose(handle);
continue;
}
native_handle_list[native_handle_count++] = handle;
}
return native_handle_count;
}
static void
unregister_and_unload_native_libs(uint32 native_lib_count,
void **native_handle_list)
{
uint32 i, n_native_symbols;
NativeSymbol *native_symbols;
char *module_name;
void *handle;
for (i = 0; i < native_lib_count; i++) {
handle = native_handle_list[i];
/* lookup get_native_lib func */
get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
if (!get_native_lib) {
LOG_WARNING("warning: failed to lookup `get_native_lib` function "
"from native lib %p",
handle);
continue;
}
n_native_symbols = get_native_lib(&module_name, &native_symbols);
if (n_native_symbols == 0 || module_name == NULL
|| native_symbols == NULL) {
LOG_WARNING("warning: get_native_lib returned different values for "
"native lib %p",
handle);
continue;
}
/* unregister native symbols */
if (!wasm_runtime_unregister_natives(module_name, native_symbols)) {
LOG_WARNING("warning: failed to unregister native lib %p", handle);
continue;
}
dlclose(handle);
}
}
#endif
/* clang-format off */
static void
print_help()
{
printf("Usage: wamrc [options] -o output_file wasm_file\n");
printf(" --target=<arch-name> Set the target arch, which has the general format: <arch><sub>\n");
printf(" <arch> = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n");
printf(" riscv64, riscv32.\n");
printf(" Default is host arch, e.g. x86_64\n");
printf(" <sub> = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n");
printf(" Use --target=help to list supported targets\n");
printf(" Or, provide a triple in the format of <arch>-<vendor>-<os>-<abi>.\n");
printf(" By doing this, --target-abi, --cpu, and --cpu-features will be ignored.\n");
printf(" The triple will only be normalized without any further verification.\n");
printf(" --target-abi=<abi> Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n");
printf(" Default is gnu if target isn't riscv64 or riscv32\n");
printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n");
printf(" Use --target-abi=help to list all the ABI supported\n");
printf(" --cpu=<cpu> Set the target CPU (default: host CPU, e.g. skylake)\n");
printf(" Use --cpu=help to list all the CPU supported\n");
printf(" --cpu-features=<features> Enable or disable the CPU features\n");
printf(" Use +feature to enable a feature, or -feature to disable it\n");
printf(" For example, --cpu-features=+feature1,-feature2\n");
printf(" Use --cpu-features=+help to list all the features supported\n");
printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n");
printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n");
printf(" 0 - Large code model\n");
printf(" 1 - Medium code model\n");
printf(" 2 - Kernel code model\n");
printf(" 3 - Small code model\n");
printf(" -sgx Generate code for SGX platform (Intel Software Guard Extensions)\n");
printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n");
printf(" This flag controls bounds checking with a software check. \n");
printf(" On 64-bit platforms, it is disabled by default, using a hardware \n");
printf(" trap if supported, except when SGX or memory64 is enabled,\n");
printf(" which defaults to a software check.\n");
printf(" On 32-bit platforms, the flag is enabled by default, using a software check\n");
printf(" due to the lack of hardware support.\n");
printf(" CAVEAT: --bounds-checks=0 enables some optimizations\n");
printf(" which make the compiled AOT module incompatible\n");
printf(" with a runtime without the hardware bounds checks.\n");
printf(" --stack-bounds-checks=1/0 Enable or disable the bounds checks for native stack:\n");
printf(" if the option isn't set, the status is same as `--bounds-check`,\n");
printf(" if the option is set, the status is same as the option value\n");
printf(" --stack-usage=<file> Generate a stack-usage file.\n");
printf(" Similarly to `clang -fstack-usage`.\n");
printf(" --format=<format> Specifies the format of the output file\n");
printf(" The format supported:\n");
printf(" aot (default) AoT file\n");
printf(" object Native object file\n");
printf(" llvmir-unopt Unoptimized LLVM IR\n");
printf(" llvmir-opt Optimized LLVM IR\n");
printf(" --disable-bulk-memory Disable the MVP bulk memory feature\n");
printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n");
printf(" thread-mgr will be enabled automatically\n");
printf(" --enable-tail-call Enable the post-MVP tail call feature\n");
printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n");
printf(" currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
printf(" and by default it is enabled in them and disabled in other targets\n");
printf(" --disable-ref-types Disable the MVP reference types feature, it will be disabled forcibly if\n");
printf(" GC is enabled\n");
printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
printf(" --enable-dump-call-stack Enable stack trace feature\n");
printf(" --call-stack-features=<features>\n");
printf(" A comma-separated list of features when generating call stacks.\n");
printf(" By default, all features are enabled. To disable all features,\n");
printf(" provide an empty list (i.e. --call-stack-features=). This flag\n");
printf(" only only takes effect when --enable-dump-call-stack is set.\n");
printf(" Available features: bounds-checks, ip, func-idx, trap-ip, values.\n");
printf(" --enable-perf-profiling Enable function performance profiling\n");
printf(" --enable-memory-profiling Enable memory usage profiling\n");
printf(" --xip A shorthand of --enable-indirect-mode --disable-llvm-intrinsics\n");
printf(" --enable-indirect-mode Enable call function through symbol table but not direct call\n");
printf(" --enable-gc Enable GC (Garbage Collection) feature\n");
printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
printf(" --enable-builtin-intrinsics=<flags>\n");
printf(" Enable the specified built-in intrinsics, it will override the default\n");
printf(" settings. It only takes effect when --disable-llvm-intrinsics is set.\n");
printf(" Available flags: all, i32.common, i64.common, f32.common, f64.common,\n");
printf(" i32.clz, i32.ctz, etc, refer to doc/xip.md for full list\n");
printf(" Use comma to separate, please refer to doc/xip.md for full list.\n");
printf(" --disable-llvm-jump-tables Disable the LLVM jump tables similarly to clang's -fno-jump-tables\n");
printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
printf(" --enable-llvm-pgo Enable LLVM PGO (Profile-Guided Optimization)\n");
printf(" --enable-llvm-passes=<passes>\n");
printf(" Enable the specified LLVM passes, using comma to separate\n");
printf(" --use-prof-file=<file> Use profile file collected by LLVM PGO (Profile-Guided Optimization)\n");
printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of linear memory,\n");
printf(" only available on linux x86-64, which may improve performance,\n");
printf(" flags can be: i32.load, i64.load, f32.load, f64.load, v128.load,\n");
printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n");
printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
printf(" and --enable-segue means all flags are added.\n");
printf(" --emit-custom-sections=<section names>\n");
printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
printf(" multiple names, e.g.\n");
printf(" --emit-custom-sections=section1,section2,sectionN\n");
#if BH_HAS_DLFCN
printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
printf(" are shared object (.so) files, for example:\n");
printf(" --native-lib=test1.so --native-lib=test2.so\n");
#endif
printf(" --invoke-c-api-import Treat unknown import function as wasm-c-api import function and\n");
printf(" quick call it from AOT code\n");
#if WASM_ENABLE_LINUX_PERF != 0
printf(" --enable-linux-perf Enable linux perf support\n");
#endif
printf(" --mllvm=<option> Add the LLVM command line option\n");
printf(" --enable-shared-heap Enable shared heap feature\n");
printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
printf(" --version Show version information\n");
printf("Examples: wamrc -o test.aot test.wasm\n");
printf(" wamrc --target=i386 -o test.aot test.wasm\n");
printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
printf(" wamrc --target-abi=help\n");
printf(" wamrc --target=x86_64 --cpu=help\n");
}
/* clang-format on */
#define PRINT_HELP_AND_EXIT() \
do { \
print_help(); \
goto fail0; \
} while (0)
/**
* Split a string into an array of strings
* Returns NULL on failure
* Memory must be freed by caller
* Based on: http://stackoverflow.com/a/11198630/471795
*/
static char **
split_string(char *str, int *count, const char *delimer)
{
char **res = NULL, **res1;
char *p;
int idx = 0;
/* split string and append tokens to 'res' */
do {
p = strtok(str, delimer);
str = NULL;
res1 = res;
res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
if (res == NULL) {
free(res1);
return NULL;
}
res[idx++] = p;
} while (p);
/**
* Due to the section name,
* res[0] might contain a '\' to indicate a space
* func\name -> func name
*/
p = strchr(res[0], '\\');
while (p) {
*p = ' ';
p = strchr(p, '\\');
}
if (count) {
*count = idx - 1;
}
return res;
}
static bool
parse_call_stack_features(char *features_str,
AOTCallStackFeatures *out_features)
{
int size = 0;
char **features;
bool ret = true;
bh_assert(features_str);
bh_assert(out_features);
/* non-empty feature list */
features = split_string(features_str, &size, ",");
if (!features) {
return false;
}
while (size--) {
if (!strcmp(features[size], "bounds-checks")) {
out_features->bounds_checks = true;
}
else if (!strcmp(features[size], "ip")) {
out_features->ip = true;
}
else if (!strcmp(features[size], "trap-ip")) {
out_features->trap_ip = true;
}
else if (!strcmp(features[size], "values")) {
out_features->values = true;
}
else if (!strcmp(features[size], "func-idx")) {
out_features->func_idx = true;
}
else {
ret = false;
printf("Unsupported feature %s\n", features[size]);
goto finish;
}
}
finish:
free(features);
return ret;
}
static bool
can_enable_tiny_frame(const AOTCompOption *opt)
{
return !opt->call_stack_features.values && !opt->enable_gc
&& !opt->enable_perf_profiling;
}
static uint32
resolve_segue_flags(char *str_flags)
{
uint32 segue_flags = 0;
int32 flag_count, i;
char **flag_list;
flag_list = split_string(str_flags, &flag_count, ",");
if (flag_list) {
for (i = 0; i < flag_count; i++) {
if (!strcmp(flag_list[i], "i32.load")) {
segue_flags |= 1 << 0;
}
else if (!strcmp(flag_list[i], "i64.load")) {
segue_flags |= 1 << 1;
}
else if (!strcmp(flag_list[i], "f32.load")) {
segue_flags |= 1 << 2;
}
else if (!strcmp(flag_list[i], "f64.load")) {
segue_flags |= 1 << 3;
}
else if (!strcmp(flag_list[i], "v128.load")) {
segue_flags |= 1 << 4;
}
else if (!strcmp(flag_list[i], "i32.store")) {
segue_flags |= 1 << 8;
}
else if (!strcmp(flag_list[i], "i64.store")) {
segue_flags |= 1 << 9;
}
else if (!strcmp(flag_list[i], "f32.store")) {
segue_flags |= 1 << 10;
}
else if (!strcmp(flag_list[i], "f64.store")) {
segue_flags |= 1 << 11;
}
else if (!strcmp(flag_list[i], "v128.store")) {
segue_flags |= 1 << 12;
}
else {
/* invalid flag */
segue_flags = (uint32)-1;
break;
}
}
free(flag_list);
}
return segue_flags;
}
/* When print help info for target/cpu/target-abi/cpu-features, load this dummy
* wasm file content rather than from an input file, the dummy wasm file content
* is: magic header + version number */
static unsigned char dummy_wasm_file[8] = { 0x00, 0x61, 0x73, 0x6D,
0x01, 0x00, 0x00, 0x00 };
int
main(int argc, char *argv[])
{
char *wasm_file_name = NULL, *out_file_name = NULL;
char **llvm_options = NULL;
size_t llvm_options_count = 0;
uint8 *wasm_file = NULL;
uint32 wasm_file_size;
wasm_module_t wasm_module = NULL;
aot_comp_data_t comp_data = NULL;
aot_comp_context_t comp_ctx = NULL;
RuntimeInitArgs init_args;
AOTCompOption option = { 0 };
char error_buf[128];
int log_verbose_level = 2;
bool sgx_mode = false, size_level_set = false, use_dummy_wasm = false;
int exit_status = EXIT_FAILURE;
#if BH_HAS_DLFCN
const char *native_lib_list[8] = { NULL };
uint32 native_lib_count = 0;
void *native_handle_list[8] = { NULL };
uint32 native_handle_count = 0;
#endif
#if WASM_ENABLE_LINUX_PERF != 0
bool enable_linux_perf = false;
#endif
option.opt_level = 3;
option.size_level = 3;
option.output_format = AOT_FORMAT_FILE;
/* default value, enable or disable depends on the platform */
option.bounds_checks = 2;
/* default value, enable or disable depends on the platform */
option.stack_bounds_checks = 2;
option.enable_simd = true;
option.enable_aux_stack_check = true;
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 */
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-o")) {
argc--, argv++;
if (argc < 2)
PRINT_HELP_AND_EXIT();
out_file_name = argv[0];
}
else if (!strncmp(argv[0], "--target=", 9)) {
if (argv[0][9] == '\0')
PRINT_HELP_AND_EXIT();
option.target_arch = argv[0] + 9;
if (!strcmp(option.target_arch, "help")) {
use_dummy_wasm = true;
}
}
else if (!strncmp(argv[0], "--target-abi=", 13)) {
if (argv[0][13] == '\0')
PRINT_HELP_AND_EXIT();
option.target_abi = argv[0] + 13;
if (!strcmp(option.target_abi, "help")) {
use_dummy_wasm = true;
}
}
else if (!strncmp(argv[0], "--cpu=", 6)) {
if (argv[0][6] == '\0')
PRINT_HELP_AND_EXIT();
option.target_cpu = argv[0] + 6;
if (!strcmp(option.target_cpu, "help")) {
use_dummy_wasm = true;
}
}
else if (!strncmp(argv[0], "--cpu-features=", 15)) {
if (argv[0][15] == '\0')
PRINT_HELP_AND_EXIT();
option.cpu_features = argv[0] + 15;
if (!strcmp(option.cpu_features, "+help")) {
use_dummy_wasm = true;
}
}
else if (!strncmp(argv[0], "--opt-level=", 12)) {
if (argv[0][12] == '\0')
PRINT_HELP_AND_EXIT();
option.opt_level = (uint32)atoi(argv[0] + 12);
if (option.opt_level > 3)
option.opt_level = 3;
}
else if (!strncmp(argv[0], "--size-level=", 13)) {
if (argv[0][13] == '\0')
PRINT_HELP_AND_EXIT();
option.size_level = (uint32)atoi(argv[0] + 13);
if (option.size_level > 3)
option.size_level = 3;
size_level_set = true;
}
else if (!strcmp(argv[0], "-sgx")) {
sgx_mode = true;
}
else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
}
else if (!strncmp(argv[0], "--stack-bounds-checks=", 22)) {
option.stack_bounds_checks = (atoi(argv[0] + 22) == 1) ? 1 : 0;
}
else if (!strncmp(argv[0], "--stack-usage=", 14)) {
option.stack_usage_file = argv[0] + 14;
}
else if (!strncmp(argv[0], "--format=", 9)) {
if (argv[0][9] == '\0')
PRINT_HELP_AND_EXIT();
if (!strcmp(argv[0] + 9, "aot"))
option.output_format = AOT_FORMAT_FILE;
else if (!strcmp(argv[0] + 9, "object"))
option.output_format = AOT_OBJECT_FILE;
else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
option.output_format = AOT_LLVMIR_UNOPT_FILE;
else if (!strcmp(argv[0] + 9, "llvmir-opt"))
option.output_format = AOT_LLVMIR_OPT_FILE;
else {
printf("Invalid format %s.\n", argv[0] + 9);
PRINT_HELP_AND_EXIT();
}
}
else if (!strncmp(argv[0], "-v=", 3)) {
log_verbose_level = atoi(argv[0] + 3);
if (log_verbose_level < 0 || log_verbose_level > 5)
PRINT_HELP_AND_EXIT();
}
else if (!strcmp(argv[0], "--disable-bulk-memory")) {
option.enable_bulk_memory = false;
}
else if (!strcmp(argv[0], "--enable-multi-thread")) {
option.enable_bulk_memory = true;
option.enable_thread_mgr = true;
}
else if (!strcmp(argv[0], "--enable-tail-call")) {
option.enable_tail_call = true;
}
else if (!strcmp(argv[0], "--enable-simd")) {
/* obsolete option, kept for compatibility */
option.enable_simd = true;
}
else if (!strcmp(argv[0], "--disable-simd")) {
option.enable_simd = false;
}
else if (!strcmp(argv[0], "--disable-ref-types")) {
option.enable_ref_types = false;
}
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;
}
else if (!strncmp(argv[0], "--call-stack-features=", 22)) {
/* Reset all the features, only enable the user-defined ones */
memset(&option.call_stack_features, 0,
sizeof(AOTCallStackFeatures));
if (argv[0][22] != '\0') {
if (!parse_call_stack_features(argv[0] + 22,
&option.call_stack_features)) {
printf("Failed to parse call-stack-features\n");
PRINT_HELP_AND_EXIT();
}
}
}
else if (!strcmp(argv[0], "--enable-perf-profiling")) {
option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
option.enable_perf_profiling = true;
}
else if (!strcmp(argv[0], "--enable-memory-profiling")) {
option.enable_memory_profiling = true;
option.enable_stack_estimation = true;
}
else if (!strcmp(argv[0], "--xip")) {
option.is_indirect_mode = true;
option.disable_llvm_intrinsics = true;
}
else if (!strcmp(argv[0], "--enable-indirect-mode")) {
option.is_indirect_mode = true;
}
else if (!strcmp(argv[0], "--enable-gc")) {
option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
option.enable_gc = true;
}
else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
option.disable_llvm_intrinsics = true;
}
else if (!strncmp(argv[0], "--enable-builtin-intrinsics=", 28)) {
if (argv[0][28] == '\0')
PRINT_HELP_AND_EXIT();
option.builtin_intrinsics = argv[0] + 28;
}
else if (!strcmp(argv[0], "--disable-llvm-jump-tables")) {
option.disable_llvm_jump_tables = true;
}
else if (!strcmp(argv[0], "--disable-llvm-lto")) {
option.disable_llvm_lto = true;
}
else if (!strcmp(argv[0], "--enable-llvm-pgo")) {
option.enable_llvm_pgo = true;
}
else if (!strncmp(argv[0], "--enable-llvm-passes=", 21)) {
if (argv[0][21] == '\0')
PRINT_HELP_AND_EXIT();
option.llvm_passes = argv[0] + 21;
}
else if (!strncmp(argv[0], "--use-prof-file=", 16)) {
if (argv[0][16] == '\0')
PRINT_HELP_AND_EXIT();
option.use_prof_file = argv[0] + 16;
}
else if (!strcmp(argv[0], "--enable-segue")) {
/* all flags are enabled */
option.segue_flags = 0x1F1F;
}
else if (!strncmp(argv[0], "--enable-segue=", 15)) {
option.segue_flags = resolve_segue_flags(argv[0] + 15);
if (option.segue_flags == (uint32)-1)
PRINT_HELP_AND_EXIT();
}
else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
int len = 0;
if (option.custom_sections) {
free(option.custom_sections);
}
option.custom_sections = split_string(argv[0] + 23, &len, ",");
if (!option.custom_sections) {
printf("Failed to process emit-custom-sections: alloc "
"memory failed\n");
PRINT_HELP_AND_EXIT();
}
option.custom_sections_count = len;
}
#if BH_HAS_DLFCN
else if (!strncmp(argv[0], "--native-lib=", 13)) {
if (argv[0][13] == '\0')
PRINT_HELP_AND_EXIT();
if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
printf("Only allow max native lib number %d\n",
(int)(sizeof(native_lib_list) / sizeof(char *)));
goto fail0;
}
native_lib_list[native_lib_count++] = argv[0] + 13;
}
#endif
else if (!strcmp(argv[0], "--invoke-c-api-import")) {
option.quick_invoke_c_api_import = true;
}
#if WASM_ENABLE_LINUX_PERF != 0
else if (!strcmp(argv[0], "--enable-linux-perf")) {
enable_linux_perf = true;
}
#endif
else if (!strncmp(argv[0], "--mllvm=", 8)) {
void *np;
if (argv[0][8] == '\0')
PRINT_HELP_AND_EXIT();
if (llvm_options_count == 0)
llvm_options_count += 2;
else
llvm_options_count++;
np = realloc(llvm_options, llvm_options_count * sizeof(char *));
if (np == NULL) {
printf("Memory allocation failure\n");
goto fail0;
}
llvm_options = np;
if (llvm_options_count == 2)
llvm_options[llvm_options_count - 2] = "wamrc";
llvm_options[llvm_options_count - 1] = argv[0] + 8;
}
else if (!strcmp(argv[0], "--enable-shared-heap")) {
option.enable_shared_heap = true;
}
else if (!strcmp(argv[0], "--version")) {
uint32 major, minor, patch;
wasm_runtime_get_version(&major, &minor, &patch);
printf("wamrc %u.%u.%u\n", major, minor, patch);
return 0;
}
else
PRINT_HELP_AND_EXIT();
}
if (!use_dummy_wasm && (argc == 0 || !out_file_name))
PRINT_HELP_AND_EXIT();
if (option.aux_stack_frame_type == AOT_STACK_FRAME_TYPE_STANDARD
&& can_enable_tiny_frame(&option)) {
LOG_VERBOSE("Use tiny frame mode for stack frames");
option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_TINY;
/* for now we only enable frame per function for a TINY frame mode */
option.call_stack_features.frame_per_function = true;
}
if (!option.call_stack_features.func_idx
&& (option.enable_gc || option.enable_perf_profiling)) {
LOG_WARNING("'func-idx' call stack feature will be automatically "
"enabled for GC and perf profiling mode");
option.call_stack_features.func_idx = true;
}
if (!size_level_set) {
/**
* Set opt level to 1 by default for Windows and MacOS as
* they can not memory map out 0-2GB memory and might not
* be able to meet the requirements of some AOT relocation
* operations.
*/
if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
LOG_VERBOSE("Set size level to 1 for Windows AOT file");
option.size_level = 1;
}
#if defined(_WIN32) || defined(_WIN32_) \
|| ((defined(__APPLE__) || defined(__MACH__)) && !defined(__arm64__))
if (!option.target_arch && !option.target_abi) {
LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
option.size_level = 1;
}
#endif
}
if (option.enable_gc && !option.call_stack_features.values) {
LOG_WARNING("Call stack feature 'values' must be enabled for GC. The "
"feature will be enabled automatically.");
option.call_stack_features.values = true;
}
if (sgx_mode) {
option.size_level = 0;
option.is_sgx_platform = true;
}
if (option.enable_gc) {
option.enable_ref_types = false;
}
if (!use_dummy_wasm) {
wasm_file_name = argv[0];
if (!strcmp(wasm_file_name, out_file_name)) {
printf("Error: input file and output file are the same");
return -1;
}
}
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
#if WASM_ENABLE_LINUX_PERF != 0
init_args.enable_linux_perf = enable_linux_perf;
#endif
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {
printf("Init runtime environment failed.\n");
return -1;
}
bh_log_set_verbose_level(log_verbose_level);
#if BH_HAS_DLFCN
bh_print_time("Begin to load native libs");
native_handle_count = load_and_register_native_libs(
native_lib_list, native_lib_count, native_handle_list);
#endif
if (llvm_options_count > 0)
LLVMParseCommandLineOptions(llvm_options_count,
(const char **)llvm_options, "wamrc");
bh_print_time("Begin to load wasm file");
if (use_dummy_wasm) {
/* load WASM byte buffer from dummy buffer */
wasm_file_size = sizeof(dummy_wasm_file);
wasm_file = dummy_wasm_file;
}
else {
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file = (uint8 *)bh_read_file_to_buffer(wasm_file_name,
&wasm_file_size)))
goto fail1;
}
if (wasm_file_size >= 4 /* length of MAGIC NUMBER */
&& get_package_type(wasm_file, wasm_file_size)
!= Wasm_Module_Bytecode) {
printf("Invalid wasm file: magic header not detected\n");
goto fail2;
}
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
sizeof(error_buf)))) {
printf("%s\n", error_buf);
goto fail2;
}
if (!(comp_data = aot_create_comp_data(wasm_module, option.target_arch,
option.enable_gc))) {
printf("%s\n", aot_get_last_error());
goto fail3;
}
#if WASM_ENABLE_DEBUG_AOT != 0
if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
printf("%s:create dwarf extractor failed\n", wasm_file_name);
}
#endif
bh_print_time("Begin to create compile context");
if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
printf("%s\n", aot_get_last_error());
goto fail4;
}
bh_print_time("Begin to compile");
if (!aot_compile_wasm(comp_ctx)) {
printf("%s\n", aot_get_last_error());
goto fail5;
}
switch (option.output_format) {
case AOT_LLVMIR_UNOPT_FILE:
case AOT_LLVMIR_OPT_FILE:
if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
printf("%s\n", aot_get_last_error());
goto fail5;
}
break;
case AOT_OBJECT_FILE:
if (!aot_emit_object_file(comp_ctx, out_file_name)) {
printf("%s\n", aot_get_last_error());
goto fail5;
}
break;
case AOT_FORMAT_FILE:
if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
printf("%s\n", aot_get_last_error());
goto fail5;
}
break;
default:
break;
}
bh_print_time("Compile end");
printf("Compile success, file %s was generated.\n", out_file_name);
exit_status = EXIT_SUCCESS;
fail5:
/* Destroy compiler context */
aot_destroy_comp_context(comp_ctx);
fail4:
/* Destroy compile data */
aot_destroy_comp_data(comp_data);
fail3:
/* Unload WASM module */
wasm_runtime_unload(wasm_module);
fail2:
/* free the file buffer */
if (!use_dummy_wasm) {
wasm_runtime_free(wasm_file);
}
fail1:
#if BH_HAS_DLFCN
unregister_and_unload_native_libs(native_handle_count, native_handle_list);
#endif
/* Destroy runtime environment */
wasm_runtime_destroy();
fail0:
/* free option.custom_sections */
if (option.custom_sections) {
free(option.custom_sections);
}
free(llvm_options);
bh_print_time("wamrc return");
return exit_status;
}