wasmedge compile CLI

After installation, users can execute the wasmedge compile command.

The usage of the wasmedge compile command will be:

$ wasmedge compile -h
USAGE
   wasmedge compile [OPTIONS] [--] WASM WASM_SO

...

The wasmedge compile command can compile WebAssembly into native machine code (i.e., the AOT compiler). For the pure WebAssembly, the wasmedge tool will execute the WASM in interpreter mode. After compiling with the wasmedge compile AOT compiler, the wasmedge tool can execute the WASM in AOT mode which is much faster.

Options

The options of the wasmedge compile command are as follows.

  1. -h|--help: Show the help messages. Will ignore other arguments below.
  2. (Optional) --dump: Dump the LLVM IR to wasm.ll and wasm-opt.ll.
  3. (Optional) --interruptible: Generate the binary which supports interruptible execution.
  4. (Optional) Statistics information:
    • By default, the AOT-compiled WASM not supports all statistics even if the options are turned on when running the wasmedge tool.
    • Use --enable-time-measuring to generate code for enabling the statistics of time measuring in execution.
    • Use --enable-gas-measuring to generate code for enabling the statistics of gas measuring in execution.
    • Use --enable-instruction-count to generate code for enabling the statistics of counting WebAssembly instructions.
    • Or use --enable-all-statistics to generate code for enabling all of the statistics.
  5. (Optional) --generic-binary: Generate the generic binary of the current host CPU architecture.
  6. (Optional) WebAssembly proposals:
  7. (Optional) --optimize: Select the LLVM optimization level.
    • Use --optimize LEVEL to set the optimization level. The LEVEL should be one of 0, 1, 2, 3, s, or z.
    • The default value will be 2, which means O2.
  8. Input WASM file (/path/to/wasm/file).
  9. Output path (/path/to/output/file).
    • By default, the wasmedge compile command will output the universal WASM format.
    • If the specific file extension (.so on Linux, .dylib on MacOS, and .dll on Windows) is assigned in the output path, the wasmedge compile command will output the shared library format.

Example

Take the fibonacci.wasm for example. It exported a fib() function which takes a single i32 integer as the input parameter.

You can run:

wasmedge compile fibonacci.wasm fibonacci_aot.wasm

or:

wasmedge compile fibonacci.wasm fibonacci_aot.so # On Linux.

The output will be:

[2022-09-09 14:22:10.540] [info] compile start
[2022-09-09 14:22:10.541] [info] verify start
[2022-09-09 14:22:10.542] [info] optimize start
[2022-09-09 14:22:10.547] [info] codegen start
[2022-09-09 14:22:10.552] [info] output start
[2022-09-09 14:22:10.600] [info] compile done

Then you can execute the output file with wasmedge and measure the execution time:

time wasmedge --reactor fibonacci_aot.wasm fib 30

The output will be:

1346269

real    0m0.029s
user    0m0.012s
sys     0m0.014s

Then you can compare it with the interpreter mode:

time wasmedge --reactor fibonacci.wasm fib 30

The output shows that the AOT-compiled WASM is much faster than the interpreter mode:

1346269

real    0m0.442s
user    0m0.427s
sys     0m0.012s