Skip to content

Enable LoongArch target #4037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions build-scripts/build_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl
LLVM_TARGETS_TO_BUILD = [
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"'
if normal_backends
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;LoongArch;X86"'
]

# if not on ARC platform, but want to add expeirmental backend ARC as target
Expand Down Expand Up @@ -238,7 +238,7 @@ def main():
parser.add_argument(
"--platform",
type=str,
choices=["android", "arc", "darwin", "linux", "windows", "xtensa"],
choices=["android", "arc", "darwin", "linux", "windows", "xtensa", "loongarch"],
help="identify current platform",
)
parser.add_argument(
Expand All @@ -254,6 +254,7 @@ def main():
"WebAssembly",
"X86",
"Xtensa",
"LoongArch",
],
default=[],
help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
Expand Down Expand Up @@ -307,6 +308,11 @@ def main():
"repo_ssh": "[email protected]:espressif/llvm-project.git",
"branch": "xtensa_release_17.0.1",
},
"loongarch": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also add loongarch to --platform option list

"repo": "https://github.com/llvm/llvm-project.git",
"repo_ssh": "[email protected]:llvm/llvm-project.git",
"branch": "release/19.x",
},
"default": {
"repo": "https://github.com/llvm/llvm-project.git",
"repo_ssh": "[email protected]:llvm/llvm-project.git",
Expand Down
12 changes: 9 additions & 3 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ elseif (WAMR_BUILD_TARGET STREQUAL "RISCV32_ILP32F")
add_definitions(-DBUILD_TARGET_RISCV32_ILP32F)
elseif (WAMR_BUILD_TARGET STREQUAL "RISCV32_ILP32")
add_definitions(-DBUILD_TARGET_RISCV32_ILP32)
elseif (WAMR_BUILD_TARGET STREQUAL "LOONGARCH64" OR WAMR_BUILD_TARGET STREQUAL "LOONGARCH64_LP64D")
add_definitions(-DBUILD_TARGET_LOONGARCH64_LP64D)
elseif (WAMR_BUILD_TARGET STREQUAL "LOONGARCH64_LP64")
add_definitions(-DBUILD_TARGET_LOONGARCH64_LP64)
elseif (WAMR_BUILD_TARGET STREQUAL "ARC")
add_definitions(-DBUILD_TARGET_ARC)
else ()
Expand All @@ -55,7 +59,8 @@ endif ()

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
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.*")
OR WAMR_BUILD_TARGET MATCHES "AARCH64.*" OR WAMR_BUILD_TARGET MATCHES "RISCV64.*"
OR WAMR_BUILD_TARGET MATCHES "LOONGARCH64.*")
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
Expand Down Expand Up @@ -317,11 +322,12 @@ else ()
message (" Wakeup of blocking operations enabled")
endif ()
if (WAMR_BUILD_SIMD EQUAL 1)
if (NOT WAMR_BUILD_TARGET MATCHES "RISCV64.*")
if ((NOT WAMR_BUILD_TARGET MATCHES "RISCV64.*")
AND (NOT WAMR_BUILD_TARGET MATCHES "LOONGARCH64.*"))
add_definitions (-DWASM_ENABLE_SIMD=1)
message (" SIMD enabled")
else ()
message (" SIMD disabled due to not supported on target RISCV64")
message (" SIMD disabled due to not supported on target RISCV64 or LOONGARCH64")
endif ()
endif ()
if (WAMR_BUILD_AOT_STACK_FRAME EQUAL 1)
Expand Down
3 changes: 3 additions & 0 deletions build-scripts/runtime_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ endif ()
# Set WAMR_BUILD_TARGET, currently values supported:
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
# "LOONGARCH64[sub]"
if (NOT DEFINED WAMR_BUILD_TARGET)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
set (WAMR_BUILD_TARGET "AARCH64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
set (WAMR_BUILD_TARGET "RISCV64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "loongarch64")
set (WAMR_BUILD_TARGET "LOONGARCH64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
# Build as X86_64 by default in 64-bit platform
set (WAMR_BUILD_TARGET "X86_64")
Expand Down
4 changes: 4 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
&& !defined(BUILD_TARGET_RISCV32_ILP32D) \
&& !defined(BUILD_TARGET_RISCV32_ILP32F) \
&& !defined(BUILD_TARGET_RISCV32_ILP32) \
&& !defined(BUILD_TARGET_LOONGARCH64_LP64D) \
&& !defined(BUILD_TARGET_LOONGARCH64_LP64) \
&& !defined(BUILD_TARGET_ARC)
/* clang-format on */
#if defined(__x86_64__) || defined(__x86_64)
Expand Down Expand Up @@ -50,6 +52,8 @@
#define BUILD_TARGET_RISCV32_ILP32F
#elif defined(__riscv) && (__riscv_xlen == 32) && (__riscv_flen == 64)
#define BUILD_TARGET_RISCV32_ILP32D
#elif defined(__loongarch) && (__loongarch_grlen == 64)
#define BUILD_TARGET_LOONGARCH64_LP64D
#elif defined(__arc__)
#define BUILD_TARGET_ARC
#else
Expand Down
10 changes: 10 additions & 0 deletions core/iwasm/aot/aot_intrinsic.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,16 @@ aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx)
add_i64_common_intrinsics(comp_ctx);
}
}
else if (!strncmp(comp_ctx->target_arch, "loongarch", 9)) {
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_CONST);
/*
* Note: Use builtin intrinsics since hardware float operation
* will cause rodata relocation
*/
add_f32_common_intrinsics(comp_ctx);
add_f64_common_intrinsics(comp_ctx);
add_common_float_integer_conversion(comp_ctx);
}
else if (!strncmp(comp_ctx->target_arch, "xtensa", 6)) {
/*
* Note: Use builtin intrinsics since hardware float operation
Expand Down
32 changes: 31 additions & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ GET_U16_FROM_ADDR(const uint8 *p)
#define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
#define E_MACHINE_LOONGARCH 258 /* LoongArch 32/64 */
#define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */
#define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */

Expand Down Expand Up @@ -303,7 +304,9 @@ loader_mmap(uint32 size, bool prot_exec, char *error_buf, uint32 error_buf_size)

#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
|| defined(BUILD_TARGET_RISCV64_LP64) \
|| defined(BUILD_TARGET_LOONGARCH64_LP64D) \
|| defined(BUILD_TARGET_LOONGARCH64_LP64)
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX)
/* The mmapped AOT data and code in 64-bit targets had better be in
range 0 to 2G, or aot loader may fail to apply some relocations,
Expand Down Expand Up @@ -421,6 +424,9 @@ get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
case E_MACHINE_RISCV:
machine_type = "riscv";
break;
case E_MACHINE_LOONGARCH:
machine_type = "loongarch";
break;
case E_MACHINE_ARC_COMPACT:
case E_MACHINE_ARC_COMPACT2:
machine_type = "arc";
Expand Down Expand Up @@ -3081,6 +3087,23 @@ is_text_section(const char *section_name)
return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext");
}

#define R_LARCH_GOT_PC_HI20 75
#define R_LARCH_GOT_PC_LO12 76
#define R_LARCH_GOT64_PC_LO20 77
#define R_LARCH_GOT64_PC_HI12 78

#if defined(BUILD_TARGET_LOONGARCH64_LP64D) \
|| defined(BUILD_TARGET_LOONGARCH64_LP64)
static bool
is_loongarch_got_reloc(uint32 type)
{
if (type == R_LARCH_GOT_PC_HI20 || type == R_LARCH_GOT_PC_LO12
|| type == R_LARCH_GOT64_PC_LO20 || type == R_LARCH_GOT64_PC_HI12)
return true;
return false;
}
#endif

static bool
do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
char *error_buf, uint32 error_buf_size)
Expand Down Expand Up @@ -3289,6 +3312,13 @@ do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
"resolve symbol %s failed", symbol);
goto check_symbol_fail;
}
#if defined(BUILD_TARGET_LOONGARCH64_LP64D) \
|| defined(BUILD_TARGET_LOONGARCH64_LP64)
else if (is_loongarch_got_reloc(relocation->relocation_type)) {
symbol_addr =
&get_target_symbol_map(NULL)[symbol_index].symbol_addr;
}
#endif

if (symbol != symbol_buf)
wasm_runtime_free(symbol);
Expand Down
Loading
Loading