Skip to content

Commit 3fe0bf0

Browse files
committed
Merge pull request #2260 from pguyot/w14/jit-wasm
JIT: Add support for WASM32 These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 0299f17 + 9505890 commit 3fe0bf0

40 files changed

Lines changed: 6227 additions & 178 deletions

.github/workflows/wasm-build.yaml

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,52 @@ jobs:
103103
src/platforms/emscripten/build/**/*.beam
104104
retention-days: 1
105105

106+
compile_tests_jit:
107+
108+
runs-on: ubuntu-24.04
109+
container: erlang:28
110+
111+
steps:
112+
- name: Checkout repo
113+
uses: actions/checkout@v4
114+
115+
- name: Install required packages
116+
run: apt update && apt install -y gperf zlib1g-dev cmake ninja-build
117+
118+
- name: Compile AtomVM and test modules with JIT for wasm32
119+
run: |
120+
set -e
121+
mkdir build
122+
cd build
123+
cmake .. -G Ninja -DAVM_DISABLE_JIT=OFF -DAVM_JIT_TARGET_ARCH=wasm32 -DAVM_PRECOMPILED_TARGETS=wasm32
124+
# test_eavmlib does not work with wasm due to http + ssl test
125+
ninja atomvmlib-emscripten erlang_test_modules test_etest test_alisp test_estdlib hello_world test_jit
126+
127+
- name: Run JIT eunit tests
128+
run: |
129+
cd build
130+
erl -pa tests/libs/jit/beams/ libs/jit/src/beams/ libs/etest/src/beams \
131+
-noshell -s tests -s init stop
132+
133+
- name: Upload JIT test modules
134+
uses: actions/upload-artifact@v4
135+
with:
136+
name: atomvm-and-test-modules-jit
137+
path: |
138+
build/**/*.avm
139+
build/**/*.beam
140+
retention-days: 1
141+
106142
wasm_build_and_test_node:
107-
needs: compile_tests
143+
needs: [compile_tests, compile_tests_jit]
108144
runs-on: ubuntu-24.04
109145
container: emscripten/emsdk
110146

147+
strategy:
148+
fail-fast: false
149+
matrix:
150+
jit: ["", "-DAVM_DISABLE_JIT=OFF"]
151+
111152
steps:
112153
- name: Checkout repo
113154
uses: actions/checkout@v4
@@ -122,16 +163,25 @@ jobs:
122163
set -euo pipefail
123164
mkdir build
124165
cd build
125-
emcmake cmake ..
166+
emcmake cmake .. ${{ matrix.jit }}
126167
emmake make -j
127168
128-
- name: Download AtomVM and test modules
169+
- name: Download AtomVM and test modules (non-JIT)
170+
if: matrix.jit == ''
129171
uses: actions/download-artifact@v4
130172
with:
131173
name: atomvm-and-test-modules
132174
path: build
133175

134-
- name: Test
176+
- name: Download AtomVM and test modules (JIT)
177+
if: matrix.jit != ''
178+
uses: actions/download-artifact@v4
179+
with:
180+
name: atomvm-and-test-modules-jit
181+
path: build
182+
183+
- name: Test (non-JIT)
184+
if: matrix.jit == ''
135185
shell: bash
136186
working-directory: ./src/platforms/emscripten/build
137187
run: |
@@ -146,8 +196,21 @@ jobs:
146196
node src/AtomVM.js ../../../../build/tests/libs/etest/test_etest.avm
147197
node src/AtomVM.js ../../../../build/tests/erlang_tests/test_crypto.beam
148198
199+
- name: Test (JIT)
200+
if: matrix.jit != ''
201+
shell: bash
202+
working-directory: ./src/platforms/emscripten/build
203+
run: |
204+
set -euxo pipefail
205+
# Test hello_world with JIT compiler (runtime JIT compilation)
206+
node src/AtomVM.js ../../../../build/examples/erlang/hello_world.beam ../../../../build/libs/atomvmlib-emscripten-wasm32.avm
207+
# Library tests with JIT compiler (jit-wasm32.avm included in test avms via pack_test)
208+
node src/AtomVM.js ../../../../build/tests/libs/alisp/test_alisp.avm
209+
node src/AtomVM.js ../../../../build/tests/libs/estdlib/test_estdlib.avm
210+
node src/AtomVM.js ../../../../build/tests/libs/etest/test_etest.avm
211+
149212
- name: "Rename and write sha256sum (node)"
150-
if: startsWith(github.ref, 'refs/tags/')
213+
if: startsWith(github.ref, 'refs/tags/') && matrix.jit == ''
151214
shell: bash
152215
working-directory: src/platforms/emscripten/build/src
153216
run: |
@@ -160,7 +223,7 @@ jobs:
160223
161224
- name: "Release (node)"
162225
uses: softprops/action-gh-release@v1
163-
if: startsWith(github.ref, 'refs/tags/')
226+
if: startsWith(github.ref, 'refs/tags/') && matrix.jit == ''
164227
with:
165228
draft: true
166229
fail_on_unmatched_files: true
@@ -181,6 +244,7 @@ jobs:
181244
strategy:
182245
fail-fast: false
183246
matrix:
247+
jit: ["", "-DAVM_DISABLE_JIT=OFF"]
184248
language: ["javascript-typescript"]
185249

186250
steps:
@@ -191,6 +255,7 @@ jobs:
191255
run: sudo apt update -y && sudo apt install -y cmake gperf
192256

193257
- name: "Initialize CodeQL"
258+
if: matrix.jit == ''
194259
uses: github/codeql-action/init@v3
195260
with:
196261
languages: ${{matrix.language}}
@@ -204,13 +269,15 @@ jobs:
204269
set -euo pipefail
205270
mkdir build
206271
cd build
207-
emcmake cmake .. -DAVM_EMSCRIPTEN_ENV=web
272+
emcmake cmake .. -DAVM_EMSCRIPTEN_ENV=web ${{ matrix.jit }}
208273
emmake make -j
209274
210275
- name: "Perform CodeQL Analysis"
276+
if: matrix.jit == ''
211277
uses: github/codeql-action/analyze@v3
212278

213279
- name: Upload wasm build for web
280+
if: matrix.jit == ''
214281
uses: actions/upload-artifact@v4
215282
with:
216283
name: atomvm-js-web

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.avm
77
*.beam
88
*.o
9+
erl_crash.dump
910
build/**
1011
xcode/**
1112
examples/**/build

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- Added Erlang distribution over serial (uart)
11+
- Added WASM32 JIT backend for Emscripten platform
1112

1213
### Fixed
1314
- Stop using deprecated `term_from_int32` on STM32 platform

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ if (NOT AVM_DISABLE_JIT AND NOT DEFINED AVM_JIT_TARGET_ARCH)
6666
endif()
6767
endif()
6868

69-
set(AVM_PRECOMPILED_TARGETS "x86_64;aarch64;arm32;armv6m;armv6m+float32;armv6m+thumb2;riscv32;riscv64" CACHE STRING "Targets to precompile code to if AVM_DISABLE_JIT is OFF or AVM_ENABLE_PRECOMPILED is ON")
69+
set(AVM_PRECOMPILED_TARGETS "x86_64;aarch64;arm32;armv6m;armv6m+float32;armv6m+thumb2;riscv32;riscv64;wasm32" CACHE STRING "Targets to precompile code to if AVM_DISABLE_JIT is OFF or AVM_ENABLE_PRECOMPILED is ON")
70+
71+
# DWARF is not supported on wasm32
72+
if (NOT AVM_DISABLE_JIT_DWARF)
73+
if (AVM_JIT_TARGET_ARCH STREQUAL "wasm32")
74+
message(FATAL_ERROR "DWARF is not supported on wasm32")
75+
endif()
76+
if ("wasm32" IN_LIST AVM_PRECOMPILED_TARGETS)
77+
list(REMOVE_ITEM AVM_PRECOMPILED_TARGETS "wasm32")
78+
endif()
79+
endif()
7080

7181
if((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") OR
7282
(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR

doc/src/jit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The JIT compiler supports the following target architectures:
2525
* `armv6m+thumb2` — ARM Cortex-M3+ with Thumb-2 support, ARMv7-M or later (Raspberry Pi Pico 2, STM32 with Cortex-M3/M4/M7/M33)
2626
* `riscv32` — 32-bit RISC-V
2727
* `riscv64` — 64-bit RISC-V
28+
* `wasm32` — WebAssembly (Emscripten)
2829

2930
### Requirements
3031

libs/jit/include/jit.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
-define(JIT_ARCH_RISCV32, 4).
3131
-define(JIT_ARCH_RISCV64, 5).
3232
-define(JIT_ARCH_ARM32, 6).
33+
-define(JIT_ARCH_WASM32, 7).
3334

3435
-define(JIT_VARIANT_PIC, 1).
3536
-define(JIT_VARIANT_FLOAT32, 2).

libs/jit/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ set(ERLANG_MODULES
4242
jit_riscv32_asm
4343
jit_riscv64
4444
jit_riscv64_asm
45+
jit_wasm32
46+
jit_wasm32_asm
4547
jit_x86_64
4648
jit_x86_64_asm
4749
)

0 commit comments

Comments
 (0)