Skip to content

[Offload] Add support for loongarch64 to host plugin #120173

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

Conversation

wangleiat
Copy link
Contributor

@wangleiat wangleiat commented Dec 17, 2024

This adds support for the loongarch64 architecture to the offload host
plugin.

Similar to #115773

To fix some test issues, I've had to add the LoongArch64 target to:

  • CompilerInvocation::ParseLangArgs
  • linkDevice in ClangLinuxWrapper.cpp
  • OMPContext::OMPContext (to set the device_kind_cpu trait)

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2024

@llvm/pr-subscribers-flang-openmp
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-offload

Author: wanglei (wangleiat)

Changes

This adds support for the loongarch64 architecture to the offload host
plugin.

Similar to #115773


Full diff: https://github.com/llvm/llvm-project/pull/120173.diff

5 Files Affected:

  • (modified) offload/CMakeLists.txt (+2)
  • (modified) offload/plugins-nextgen/common/src/Utils/ELF.cpp (+2)
  • (modified) offload/plugins-nextgen/host/CMakeLists.txt (+5-1)
  • (modified) offload/plugins-nextgen/host/dynamic_ffi/ffi.h (+1-1)
  • (modified) offload/plugins-nextgen/host/src/rtl.cpp (+2)
diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index 8da95c5c74e445..f6e894d3996859 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -207,6 +207,8 @@ set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} s390x-ibm-linux-gnu")
 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} s390x-ibm-linux-gnu-LTO")
 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux-gnu")
 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux-gnu-LTO")
+set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu")
+set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu-LTO")
 
 # Once the plugins for the different targets are validated, they will be added to
 # the list of supported targets in the current system.
diff --git a/offload/plugins-nextgen/common/src/Utils/ELF.cpp b/offload/plugins-nextgen/common/src/Utils/ELF.cpp
index 10b32440dc8778..44d1c737e2efb9 100644
--- a/offload/plugins-nextgen/common/src/Utils/ELF.cpp
+++ b/offload/plugins-nextgen/common/src/Utils/ELF.cpp
@@ -47,6 +47,8 @@ uint16_t utils::elf::getTargetMachine() {
   return EM_PPC64;
 #elif defined(__riscv)
   return EM_RISCV;
+#elif defined(__loongarch__)
+  return EM_LOONGARCH;
 #else
 #warning "Unknown ELF compilation target architecture"
   return EM_NONE;
diff --git a/offload/plugins-nextgen/host/CMakeLists.txt b/offload/plugins-nextgen/host/CMakeLists.txt
index cbfe4b951af453..e6b3bdf83d7408 100644
--- a/offload/plugins-nextgen/host/CMakeLists.txt
+++ b/offload/plugins-nextgen/host/CMakeLists.txt
@@ -1,4 +1,4 @@
-set(supported_targets x86_64 aarch64 ppc64 ppc64le riscv64 s390x)
+set(supported_targets x86_64 aarch64 ppc64 ppc64le riscv64 s390x loongarch64)
 if(NOT ${CMAKE_SYSTEM_PROCESSOR} IN_LIST supported_targets)
   message(STATUS "Not building ${machine} NextGen offloading plugin")
   return()
@@ -63,4 +63,8 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "riscv64$")
   list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
        "riscv64-unknown-linux-gnu" "riscv64-unknown-linux-gnu-LTO")
   set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
+elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "loongarch64$")
+  list(APPEND LIBOMPTARGET_SYSTEM_TARGETS
+       "loongarch64-unknown-linux-gnu" "loongarch64-unknown-linux-gnu-LTO")
+  set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}" PARENT_SCOPE)
 endif()
diff --git a/offload/plugins-nextgen/host/dynamic_ffi/ffi.h b/offload/plugins-nextgen/host/dynamic_ffi/ffi.h
index 8b4e0286d65e3c..4a9e88cc4dc9cc 100644
--- a/offload/plugins-nextgen/host/dynamic_ffi/ffi.h
+++ b/offload/plugins-nextgen/host/dynamic_ffi/ffi.h
@@ -44,7 +44,7 @@ typedef enum ffi_abi {
 #if (defined(_M_X64) || defined(__x86_64__))
   FFI_DEFAULT_ABI = 2, // FFI_UNIX64.
 #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) ||       \
-    defined(__riscv)
+    defined(__riscv) || defined(__loongarch__)
   FFI_DEFAULT_ABI = 1, // FFI_SYSV.
 #elif defined(__powerpc64__)
   FFI_DEFAULT_ABI = 8, // FFI_LINUX.
diff --git a/offload/plugins-nextgen/host/src/rtl.cpp b/offload/plugins-nextgen/host/src/rtl.cpp
index 915c41e88c5828..171c202e3e39ea 100644
--- a/offload/plugins-nextgen/host/src/rtl.cpp
+++ b/offload/plugins-nextgen/host/src/rtl.cpp
@@ -442,6 +442,8 @@ struct GenELF64PluginTy final : public GenericPluginTy {
 #endif
 #elif defined(__riscv) && (__riscv_xlen == 64)
     return llvm::Triple::riscv64;
+#elif defined(__loongarch__) && (__loongarch_grlen == 64)
+    return llvm::Triple::loongarch64;
 #else
     return llvm::Triple::UnknownArch;
 #endif

@jhuber6
Copy link
Contributor

jhuber6 commented Dec 17, 2024

Does this actually work?

Created using spr 1.3.5-bogner
@llvmbot llvmbot added clang Clang issues not falling into any other category flang:openmp clang:openmp OpenMP related changes to Clang labels Dec 17, 2024
@wangleiat
Copy link
Contributor Author

wangleiat commented Dec 17, 2024

Does this actually work?

I executed the ninja check-offload command on LoongArch64, and the results are as follows:

Befor commit:

Total Discovered Tests: 658
Unsupported: 320 (48.63%)  
Passed     :  30 (4.56%)   
Failed     : 308 (46.81%)  

After:

Total Discovered Tests: 658
Unsupported: 320 (48.63%)  
Passed     : 296 (44.98%)  
Failed     :  42 (6.38%)   

Based on these results, the main functionality should be working correctly on loongarch64.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Thanks, I don't have any way to test this but it's pretty straightforward.

@jplehr
Copy link
Contributor

jplehr commented Dec 17, 2024

Is there the possibility to set up a buildbot for this architecture?

@wangleiat
Copy link
Contributor Author

Is there the possibility to set up a buildbot for this architecture?

Hi @SixWeining, do we have any plans to add more buildbots?

@SixWeining
Copy link
Contributor

Is there the possibility to set up a buildbot for this architecture?

There is a buildbot for loongarch, but it only run tests for clang and llvm-test-suite.

Do you mean adding a dedicate buildbot for the offload sub-project? I think the answer is yes. Actually we plan to setup more buildbots for any other sub-projects, including flang, lld and openmp, etc.

@jplehr
Copy link
Contributor

jplehr commented Dec 17, 2024

Do you mean adding a dedicate buildbot for the offload sub-project? I think the answer is yes. Actually we plan to setup more buildbots for any other sub-projects, including flang, lld and openmp, etc.

Not necessarily a dedicated buildbot for the offload project, but some bot that covers this particular code path. So it's great to hear that you have plans to put up bot(s) for the various projects.

@wangleiat wangleiat merged commit bdf7270 into main Dec 17, 2024
11 checks passed
@wangleiat wangleiat deleted the users/wangleiat/spr/offload-add-support-for-loongarch64-to-host-plugin branch December 17, 2024 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category flang:openmp offload
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants