Skip to content

Commit c357556

Browse files
committed
Add RISC-V 64-bit JIT backend, tests, and CI integration
Implement jit_riscv64.erl backend with full JIT compilation support: - RV64IMAC calling convention (a0=context, s1-s11 callee-saved) - All required backend exports (new, stream, call_primitive, move_to_native_register, jump_to_label, cond_jump_to_label, etc.) - Register value caching to avoid redundant loads - Branch patching with compressed instruction support - DWARF debug info integration (jit_dwarf.erl machine type, jit_dwarf.hrl register constant) C-side integration: - Architecture detection in jit.h (__riscv with __riscv_xlen == 64) - Struct offset static assertions in jit.c - Atom registration for riscv64 in nifs.c and defaultatoms.def - CMake JIT_TARGET_ARCH=riscv64 support CI pipeline: - Cross-compiled plain, JIT, and JIT+DWARF builds - Uses crossbuild-essential-riscv64 with QEMU user emulation Test infrastructure: - jit_riscv64_tests.erl for backend operation tests - jit_tests_common.erl binutils support for riscv64-linux-gnu - Registration in tests.erl and CMakeLists.txt
1 parent 16204bf commit c357556

File tree

16 files changed

+3951
-15
lines changed

16 files changed

+3951
-15
lines changed

.github/workflows/build-and-test.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,47 @@ jobs:
411411
arch: "s390x"
412412
library-arch: s390x-linux-gnu
413413

414+
# riscv64 build
415+
- os: "ubuntu-24.04"
416+
cc: "riscv64-linux-gnu-gcc"
417+
cxx: "riscv64-linux-gnu-g++"
418+
cflags: "-O2"
419+
otp: "28"
420+
elixir_version: "1.17"
421+
rebar3_version: "3.24.0"
422+
cmake_opts_other: "-DAVM_WARNINGS_ARE_ERRORS=ON -DCMAKE_TOOLCHAIN_FILE=${RUNNER_TEMP}/riscv64_toolchain.cmake"
423+
compiler_pkgs: "crossbuild-essential-riscv64 libc6-dbg:riscv64 zlib1g-dev:riscv64 libmbedtls-dev:riscv64 qemu-user qemu-user-binfmt binfmt-support"
424+
arch: "riscv64"
425+
library-arch: riscv64-linux-gnu
426+
427+
# riscv64 build + jit
428+
- os: "ubuntu-24.04"
429+
cc: "riscv64-linux-gnu-gcc"
430+
cxx: "riscv64-linux-gnu-g++"
431+
cflags: "-O2"
432+
otp: "28"
433+
elixir_version: "1.17"
434+
rebar3_version: "3.24.0"
435+
cmake_opts_other: "-DAVM_DISABLE_JIT=OFF -DAVM_JIT_TARGET_ARCH=riscv64 -DCMAKE_TOOLCHAIN_FILE=${RUNNER_TEMP}/riscv64_toolchain.cmake"
436+
compiler_pkgs: "crossbuild-essential-riscv64 libc6-dbg:riscv64 zlib1g-dev:riscv64 libmbedtls-dev:riscv64 qemu-user qemu-user-binfmt binfmt-support"
437+
arch: "riscv64"
438+
library-arch: riscv64-linux-gnu
439+
jit_target_arch: "riscv64"
440+
441+
# JIT + DWARF build (riscv64)
442+
- os: "ubuntu-24.04"
443+
cc: "riscv64-linux-gnu-gcc"
444+
cxx: "riscv64-linux-gnu-g++"
445+
cflags: "-O2"
446+
otp: "28"
447+
elixir_version: "1.17"
448+
rebar3_version: "3.24.0"
449+
cmake_opts_other: "-DAVM_DISABLE_JIT=OFF -DAVM_DISABLE_JIT_DWARF=OFF -DAVM_JIT_TARGET_ARCH=riscv64 -DCMAKE_TOOLCHAIN_FILE=${RUNNER_TEMP}/riscv64_toolchain.cmake"
450+
compiler_pkgs: "crossbuild-essential-riscv64 libc6-dbg:riscv64 zlib1g-dev:riscv64 libmbedtls-dev:riscv64 qemu-user qemu-user-binfmt binfmt-support"
451+
arch: "riscv64"
452+
library-arch: riscv64-linux-gnu
453+
jit_target_arch: "riscv64"
454+
414455
# riscv32-ilp32 build
415456
- os: "ubuntu-24.04"
416457
cc: "riscv32-unknown-linux-gnu-gcc"

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ if (NOT AVM_DISABLE_JIT AND NOT DEFINED AVM_JIT_TARGET_ARCH)
6161
set(AVM_JIT_TARGET_ARCH "aarch64")
6262
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^cortex-m.+$")
6363
set(AVM_JIT_TARGET_ARCH "armv6m")
64+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^riscv64$")
65+
set(AVM_JIT_TARGET_ARCH "riscv64")
6466
else()
6567
message(FATAL_ERROR "JIT is not supported on ${CMAKE_SYSTEM_PROCESSOR}")
6668
endif()
6769
endif()
6870

69-
set(AVM_PRECOMPILED_TARGETS "x86_64;aarch64;armv6m;armv6m+float32;riscv32" CACHE STRING "Targets to precompile code to if AVM_DISABLE_JIT is OFF or AVM_ENABLE_PRECOMPILED is ON")
71+
set(AVM_PRECOMPILED_TARGETS "x86_64;aarch64;armv6m;armv6m+float32;riscv32;riscv64" CACHE STRING "Targets to precompile code to if AVM_DISABLE_JIT is OFF or AVM_ENABLE_PRECOMPILED is ON")
7072

7173
if((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") OR
7274
(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR

libs/jit/include/jit.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
-define(JIT_ARCH_AARCH64, 2).
2929
-define(JIT_ARCH_ARMV6M, 3).
3030
-define(JIT_ARCH_RISCV32, 4).
31+
-define(JIT_ARCH_RISCV64, 5).
3132

3233
-define(JIT_VARIANT_PIC, 1).
3334
-define(JIT_VARIANT_FLOAT32, 2).

libs/jit/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ set(ERLANG_MODULES
3636
jit_armv6m_asm
3737
jit_riscv32
3838
jit_riscv32_asm
39+
jit_riscv64
40+
jit_riscv64_asm
3941
jit_x86_64
4042
jit_x86_64_asm
4143
)

libs/jit/src/jit_dwarf.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ elf(_State, _NativeCode) ->
343343
backend_to_machine_type(jit_x86_64) -> ?EM_X86_64;
344344
backend_to_machine_type(jit_aarch64) -> ?EM_AARCH64;
345345
backend_to_machine_type(jit_armv6m) -> ?EM_ARM;
346-
backend_to_machine_type(jit_riscv32) -> ?EM_RISCV.
346+
backend_to_machine_type(jit_riscv32) -> ?EM_RISCV;
347+
backend_to_machine_type(jit_riscv64) -> ?EM_RISCV.
347348

348349
%% Map JIT backend to ELF flags
349350
backend_to_elf_flags(jit_armv6m) ->

libs/jit/src/jit_dwarf.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@
105105
-define(DWARF_X0_REG_AARCH64, 0).
106106
% a0 register in RISC-V
107107
-define(DWARF_A0_REG_RISCV32, 10).
108+
-define(DWARF_A0_REG_RISCV64, 10).
108109
% r0 register in ARM
109110
-define(DWARF_R0_REG_ARMV6M, 0).

libs/jit/src/jit_precompile.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ compile(Target, Dir, Dwarf, Path) ->
119119
"aarch64" -> ?JIT_ARCH_AARCH64;
120120
"armv6m" -> ?JIT_ARCH_ARMV6M;
121121
"riscv32" -> ?JIT_ARCH_RISCV32;
122+
"riscv64" -> ?JIT_ARCH_RISCV64;
122123
_ -> error({unsupported_target, Target})
123124
end,
124125

0 commit comments

Comments
 (0)