
wasmc:可在微控制器上直接运行的 WebAssembly 到 RISC-V 编译器
来源标题:Compiling WebAssembly to RISC-V on the microcontroller itself
wasmc 是一款能在设备上直接运行的轻量级 AOT 编译器,可将 WebAssembly 1.0 模块编译为 32 位 RISC-V 原生代码。它面向 ESP32-C3 等微控制器,典型模块编译耗时仅几十毫秒、占用几 KB 堆内存。其生成的 AOT 文件可被 WAMR 2.4.4 的 AOT 运行时直接加载运行,在 ESP32-C3 上实测运行速度约为 WAMR 解释执行的 10 倍。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
wasmc is a lightweight ahead-of-time (AOT) compiler that runs directly on the device. It translates WebAssembly 1.0 (WASM) modules into native 32-bit RISC-V code (RV32IM). It is written for microcontrollers like the ESP32-C3 (400 KB of RAM): a typical module compiles in a few tens of milliseconds and a few KB of heap.
+-------------------------- on the device (e.g. ESP32-C3) --------------------------+ | | | +--------------+ +-------------+ +-------------+ | | | | wasmc_compile() | AOT file | WAMR AOT runtime | native | | | | .wasm module |---------------->| in RAM |----------------->| RV32IM code | | | | | | | | execution | | | +--------------+ +-------------+ +-------------+ | | | +-----------------------------------------------------------------------------------+
wasmc emits executables in the AOT file format of the WebAssembly Micro Runtime (WAMR) (version 2.4.4), so the files wasmc makes can be loaded and run by WAMR's AOT runtime as they are, just like the files of wamrc, WAMR's own LLVM-based AOT compiler, which runs only a PC.
On the ESP32-C3, over 47 modules from the Embench IoT and Sightglass suites, code compiled by wasmc on the board runs about 10× faster than WAMR's fast interpreter and about 18% slower than code compiled on a PC by wamrc. Its AOT files are about 3% smaller than wamrc's.
Full results, with charts and methodology: bench/esp32c3-idf/results/summary.md. How to run the benchmarks yourself: bench/esp32c3-idf/README.md.
wasmc compiles the whole of WebAssembly 1.0 (the 2019 W3C recommendation, also known as the MVP), in its binary format. It passes the official 1.0 core test suite, adapted to what a single-module AOT compiler can run (see tests/README.md).
WAMR and the benchmark suites are git submodules:
git clone --recurse-submodules https://github.com/llllluca/wasmc
or, in an existing clone:
git submodule update --init
On a Linux machine
Build
You need GCC and make:
make # builds build/wasmc and build/readaot make help # all the targets (tests, sanitizers, coverage, static analysis)
Compile a WebAssembly module
build/wasmc -o fib.aot fib.wasm build/readaot fib.aot # show and validate the AOT file
Without -o the output is a.aot, build/wasmc --help lists the options. wasmc accepts WebAssembly 1.0 only, so build modules with clang's -mcpu=mvp. Two small examples, with the build commands and the details in their comments:
clang --target=wasm32 -mcpu=mvp -O2 -nostdlib -Wl,--no-entry
-o fib.wasm examples/fib.c
examples/hello.c calls printf, which WAMR provides at run time:
clang --target=wasm32 -mcpu=mvp -O2 -nostdlib -Wl,--no-entry
-Wl,--allow-undefined -o hello.wasm examples/hello.c
Run an AOT file
The AOT files contain 32-bit RISC-V code, so on an x86-64 machine they run in a WAMR built for riscv32 Linux, inside QEMU's user-mode emulator. You need:
A riscv32 Linux GCC for the soft-float ABI (-mabi=ilp32), the ABI of wasmc's code. The prebuilt riscv32 toolchains are usually built for ilp32d and will not work, so build one from riscv-gnu-toolchain:
git clone https://github.com/riscv-collab/riscv-gnu-toolchain cd riscv-gnu-toolchain ./configure --prefix=$HOME/opt/riscv32-ilp32 --with-arch=rv32imac --with-abi=ilp32 make linux # takes a while
QEMU user mode, cmake, and wabt (for the tests). On Debian or Ubuntu:
sudo apt install qemu-user cmake wabt
Tell the Makefile where the toolchain is, once, in a config.mk file next to it (not in the repository). make iwasm and make test both read it:
echo 'RV32_CC := $(HOME)/opt/riscv32-ilp32/bin/riscv32-unknown-linux-gnu-gcc' > config.mk
Then build WAMR's iwasm from the submodule with make iwasm, and run the file:
make iwasm qemu-riscv32 build/iwasm-rv32/iwasm -f fib fib.aot 20 # prints 0x1a6d:i32 qemu-riscv32 build/iwasm-rv32/iwasm hello.aot # prints Hello World!
Run the tests
make test runs the WebAssembly 1.0 core test suite (tests/, see tests/README.md): each module is compiled by wasmc and run by WAMR's AOT runtime, all on riscv32 under QEMU. It needs the toolchain, the packages and the config.mk above:
make test
The last line should report every file passed:
[TOTAL] 74 files, 0 not fully passed 20876 checks passed
make test-asan compiles every test and benchmark module with a wasmc built with AddressSanitizer and UBSan, on the Linux machine, without QEMU.
The example in examples/esp32c3-idf/ is an ESP-IDF app that compiles four small WebAssembly programs on the board with wasmc, runs them with the WAMR AOT runtime and checks their results. You need ESP-IDF 5.5 (tested with v5.5.2) and the wasm-micro-runtime submodule:
git submodule update --init wasm-micro-runtime . $IDF_PATH/export.sh cd examples/esp32c3-idf idf.py set-target esp32c3 idf.py -p /dev/ttyUSB0 build flash monitor # on a board idf.py qemu monitor # or without one, in QEMU
The output looks like this:
fib.wasm: 100 bytes of WASM -> 564 bytes of AOT in 587 us (wasmc memory: 1404 bytes) fib(24) = 46368 ok 23649 us fib(24), native C = 46368 ok 11738 us ...
Its README also explains how to use wasmc in your own ESP-IDF project.
wasmc was developed with the help of AI. I designed the project and made the technical decisions, AI wrote much of the code, tests and documentation under my direction. Every change was reviewed and checked against the test suite.
wasmc started as my master's thesis project. The tag ccnc2027 is the code used for the thesis. Up to the ccnc2027 tag, all the code was written by hand.
wasmc is free software, released under the GNU General Public License, version 3 or later: see LICENSE. Third-party code keeps its own license: jsmn.h (MIT), examples/esp32c3-ed/wasm/ed.c (Caldera's license), and the submodules.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。