Skip to content

Commit e4023c8

Browse files
no1wudiwenyongh
andauthored
Implement AOT support for RISCV (#649)
Enable RISCV AOT support, the supported ABIs are LP64 and LP64D for riscv64, ILP32 and ILP32D for riscv32. For wamrc: use --target=riscv64/riscv32 to specify the target arch of output AOT file, use --target-abi=lp64d/lp64/ilp32d/ilp32 to specify the target ABI, if --target-abi isn't specified, by default lp64d is used for riscv64, and ilp32d is used for riscv32. Signed-off-by: Huang Qi <[email protected]> Co-authored-by: wenyongh <[email protected]>
1 parent ea06c19 commit e4023c8

29 files changed

+667
-459
lines changed

build-scripts/config_common.cmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
5151
endif ()
5252

5353
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
54-
if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64" OR WAMR_BUILD_TARGET MATCHES "AARCH64.*" OR WAMR_BUILD_TARGET MATCHES "RISCV64.*")
54+
if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64"
55+
OR WAMR_BUILD_TARGET MATCHES "AARCH64.*" OR WAMR_BUILD_TARGET MATCHES "RISCV64.*")
5556
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
5657
# Add -fPIC flag if build as 64-bit
5758
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
@@ -186,8 +187,12 @@ else ()
186187
add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=0)
187188
endif ()
188189
if (WAMR_BUILD_SIMD EQUAL 1)
189-
add_definitions (-DWASM_ENABLE_SIMD=1)
190-
message (" SIMD enabled")
190+
if (NOT WAMR_BUILD_TARGET MATCHES "RISCV64.*")
191+
add_definitions (-DWASM_ENABLE_SIMD=1)
192+
message (" SIMD enabled")
193+
else ()
194+
message (" SIMD disabled due to not supported on target RISCV64")
195+
endif ()
191196
endif ()
192197
if (WAMR_BUILD_MEMORY_PROFILING EQUAL 1)
193198
add_definitions (-DWASM_ENABLE_MEMORY_PROFILING=1)

build-scripts/runtime_lib.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ if (WAMR_BUILD_INTERP EQUAL 1 OR WAMR_BUILD_JIT EQUAL 1)
5252
include (${IWASM_DIR}/interpreter/iwasm_interp.cmake)
5353
endif ()
5454

55-
if (WAMR_BUILD_TARGET MATCHES "RISCV.*" AND WAMR_BUILD_AOT EQUAL 1)
56-
set (WAMR_BUILD_AOT 0)
57-
message ("-- WAMR AOT disabled as it isn't supported by riscv currently")
58-
endif ()
59-
6055
if (WAMR_BUILD_AOT EQUAL 1)
6156
include (${IWASM_DIR}/aot/iwasm_aot.cmake)
6257
if (WAMR_BUILD_JIT EQUAL 1)

core/app-mgr/app-manager/module_wasm_app.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
13951395
if (section->section_type == AOT_SECTION_TYPE_TEXT) {
13961396
int map_prot =
13971397
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
1398-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
1398+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
1399+
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
13991400
/* aot code and data in x86_64 must be in range 0 to 2G due to
14001401
relocation for R_X86_64_32/32S/PC32 */
14011402
int map_flags = MMAP_MAP_32BIT;

core/iwasm/aot/aot_loader.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ GET_U64_FROM_ADDR(uint32 *addr)
182182
#define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
183183
#define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
184184
#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
185+
#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
185186
#define E_MACHINE_WIN_X86_64 0x8664 /* Windowx x86-64 architecture */
186187

187188
/* Legal values for e_version */
@@ -257,6 +258,9 @@ get_aot_file_target(AOTTargetInfo *target_info,
257258
case E_MACHINE_XTENSA:
258259
machine_type = "xtensa";
259260
break;
261+
case E_MACHINE_RISCV:
262+
machine_type = "riscv";
263+
break;
260264
default:
261265
set_error_buf_v(error_buf, error_buf_size,
262266
"unknown machine type %d",
@@ -1030,7 +1034,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
10301034
/* Create each data section */
10311035
for (i = 0; i < module->data_section_count; i++) {
10321036
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
1033-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
1037+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
1038+
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
10341039
/* aot code and data in x86_64 must be in range 0 to 2G due to
10351040
relocation for R_X86_64_32/32S/PC32 */
10361041
int map_flags = MMAP_MAP_32BIT;
@@ -1501,6 +1506,7 @@ do_text_relocation(AOTModule *module,
15011506
symbol_addr = module->code;
15021507
}
15031508
else if (!strcmp(symbol, ".data")
1509+
|| !strcmp(symbol, ".sdata")
15041510
|| !strcmp(symbol, ".rdata")
15051511
|| !strcmp(symbol, ".rodata")
15061512
/* ".rodata.cst4/8/16/.." */
@@ -2235,7 +2241,8 @@ create_sections(const uint8 *buf, uint32 size,
22352241
if (section_size > 0) {
22362242
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE
22372243
| MMAP_PROT_EXEC;
2238-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
2244+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
2245+
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
22392246
/* aot code and data in x86_64 must be in range 0 to 2G due to
22402247
relocation for R_X86_64_32/32S/PC32 */
22412248
int map_flags = MMAP_MAP_32BIT;

core/iwasm/aot/aot_runtime.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,12 +2776,12 @@ aot_table_copy(AOTModuleInstance *module_inst,
27762776
/* if src_offset >= dst_offset, copy from front to back */
27772777
/* if src_offset < dst_offset, copy from back to front */
27782778
/* merge all together */
2779-
bh_memcpy_s((uint8 *)(dst_tbl_inst) + offsetof(AOTTableInstance, data)
2780-
+ dst_offset * sizeof(uint32),
2781-
(dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32),
2782-
(uint8 *)(src_tbl_inst) + offsetof(AOTTableInstance, data)
2783-
+ src_offset * sizeof(uint32),
2784-
length * sizeof(uint32));
2779+
bh_memmove_s((uint8 *)(dst_tbl_inst) + offsetof(AOTTableInstance, data)
2780+
+ dst_offset * sizeof(uint32),
2781+
(dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32),
2782+
(uint8 *)(src_tbl_inst) + offsetof(AOTTableInstance, data)
2783+
+ src_offset * sizeof(uint32),
2784+
length * sizeof(uint32));
27852785
}
27862786

27872787
void

0 commit comments

Comments
 (0)