Conversation
|
You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool. What Enabling Code Scanning Means:
For more information about GitHub Code Scanning, check out the documentation. |
cb181a7 to
65b7bd7
Compare
|
AMP: PR Review: WASM32 JIT Backend for AtomVMReviewer: AI-assisted review SummaryThis PR adds a complete WASM32 JIT backend to AtomVM, allowing JIT compilation when running under Emscripten (browser/Node.js). The key architectural decision is that WASM cannot use function pointer arithmetic like native backends, so a label-based continuation encoding ( Commits
Architecture Assessment: ✅ SoundThe
The
Issues Found🔴 Critical: Native Code Memory Leak on Module Unload
// jit_stream_wasm.c — compile_wasm_stream()
struct JITWasmHeader *header = malloc(sizeof(struct JITWasmHeader)); // (1)
header->wasm_binary = malloc(wasm_size); // (2)
header->lines_metadata = malloc(lines_data_size); // (3)
ModuleNativeEntryPoint *block = calloc(num_entries + 1, ...); // (4)But // module.c:1292-1311
COLD_FUNC void module_destroy(Module *module)
{
free(module->labels);
free(module->imported_funcs);
free(module->literals_table);
free(module->local_atoms_to_global_table);
free(module->line_refs_offsets);
// ... no mention of native_code
free(module);
}Recommendation: Add a platform hook 🔴 Critical: JS
|
| Area | Test File | Lines | Coverage |
|---|---|---|---|
| WASM instruction encoding | jit_wasm32_asm_tests.erl |
435 | All instruction types, LEB128 edge cases |
| WASM backend codegen | jit_wasm32_tests.erl |
2235 | Opcode compilation, register handling, control flow |
| Cross-arch test infra | jit_tests_common.erl |
+217 | WAT assembly validation |
| CI integration | wasm-build.yaml |
+81 | Build + hello_world + library tests under JIT |
What's Missing ❌
These test scenarios should be added to strengthen confidence in the runtime:
- Multi-thread execution: Same JITted module running on 2+ schedulers/pthreads to exercise per-thread compilation
- Thread migration on yield: Forced reduction yield + resume on a different thread — validates
saved_function_ptrlabel encoding survives cross-thread dispatch - Trap/load/resume cycle:
code_servertrap → load → resume path under WASM JIT (exercisesjit_trap_and_load+CodeServerResumeSignalhandling) - Module reload/unload loop: Repeated load/unload to detect native-code memory growth and JS handle leaks
- Stacktrace accuracy after function splits: Verify
record_continuation_lineproduces correct line info in stacktraces when a continuation label is hit
Actionable Recommendations
Must-fix before merge
- Add native-code release hook in
module_destroy()for WASM allocations - Guard NULL
native_pcaftermodule_get_native_entry_point()in the dispatch loop
Should-fix soon after merge
- Add
removeFunction()cleanup in JS cache when modules are unloaded - Store
lines_metadata_sizeinJITWasmHeaderfor bounds checking - Add multi-thread and lifecycle integration tests
Nice-to-have
- Centralize
label + 1encode/decode into helper functions to reduce#ifdefspread - Key JS cache by stable module ID instead of raw pointer to prevent stale cache issues
Overall Verdict
Architecture: ✅ The JIT_JUMPTABLE_IS_DATA + label continuation approach is the right design for WASM.
Correctness:
Tests:
Build/CI: ✅ Well-structured matrix build with both JIT and non-JIT WASM targets.
Code quality: ✅ Clean commit sequence, good separation of concerns, well-commented.
|
Just addressed the two "CRITICAL" points in AMP review, even if we don't really have module unloading for now. Also fixed an mmap leakage we had with generic unix on module destroy. |
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Add optional backend callback `supports_tail_cache/0` Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Move `decrement_reductions_and_maybe_schedule_next` before reading extended registers and is_atom checks in OP_APPLY and OP_APPLY_LAST. This matches the interpreter's order and is required for WASM. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Add current_line tracking to the compiler state and `record_continuation_line/3` to emit line info at continuation points. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
…tion Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Also reduce the size of copied / mapped data to the current architecture code for hypothetical cases where we deploy a precompiled module with several architectures Signed-off-by: Paul Guyot <pguyot@kallisys.net>
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
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