[Openmp-commits] [llvm] [openmp] [offload] Standalone build fixes (PR #118173)

Michał Górny via Openmp-commits openmp-commits at lists.llvm.org
Sat Nov 30 06:58:52 PST 2024


https://github.com/mgorny created https://github.com/llvm/llvm-project/pull/118173

A fair number of fixes to get standalone builds of offload working — mostly copying missing bits from openmp. It's almost ready — I still need to figure out why some of the tsts aren't linking to the right libraries.

>From 2c6bf6d2f88486c2ca6089692e847b45d3abdd7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 12:32:57 +0100
Subject: [PATCH 1/8] [offload] Include CheckCXXCompilerFlag in CMake

Include CheckCXXCompilerFlag before it is used in the top-level CMake
file.  This fixes standalone builds, but it is also cleaner than
assuming that some previous CMake file will include it for us.
---
 offload/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index e24f0faa912117..b464de16e8bc6d 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -129,6 +129,7 @@ include(LibomptargetGetDependencies)
 # Set up testing infrastructure.
 include(OpenMPTesting)
 
+include(CheckCXXCompilerFlag)
 check_cxx_compiler_flag(-Werror=global-constructors OFFLOAD_HAVE_WERROR_CTOR)
 
 # LLVM source tree is required at build time for libomptarget

>From e33e2bc386e9b95a9a096253bf1bf2128d1696b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 12:34:19 +0100
Subject: [PATCH 2/8] [offload] Fix building standalone against LLVM dylib

When building standalone, and only LLVM dylib is installed, there are
no LLVM* library targets.  Skip the respective dependencies if
the targets are not present, and use the standard `llvm_config()`
function to link to the dylib, provided it supplies the needed library,
with a fallback to using the static library.

Note that I had to change PluginCommon into a STATIC library,
for compatibility with `llvm_config`.
---
 offload/plugins-nextgen/amdgpu/CMakeLists.txt |  9 +++++++--
 offload/plugins-nextgen/common/CMakeLists.txt | 13 ++++++++++---
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/offload/plugins-nextgen/amdgpu/CMakeLists.txt b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
index b40c62d43226f4..79e61f2525bc8e 100644
--- a/offload/plugins-nextgen/amdgpu/CMakeLists.txt
+++ b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
@@ -8,14 +8,19 @@ target_sources(omptarget.rtl.amdgpu PRIVATE src/rtl.cpp)
 target_include_directories(omptarget.rtl.amdgpu PRIVATE
                            ${CMAKE_CURRENT_SOURCE_DIR}/utils)
 
+set(USE_SHARED)
+if (LLVM_LINK_LLVM_DYLIB)
+  set(USE_SHARED USE_SHARED)
+endif()
+llvm_config(omptarget.rtl.amdgpu ${USE_SHARED} FrontendOffloading)
+
 if(hsa-runtime64_FOUND AND NOT "amdgpu" IN_LIST LIBOMPTARGET_DLOPEN_PLUGINS)
   message(STATUS "Building AMDGPU plugin linked against libhsa")
-  target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64 LLVMFrontendOffloading)
+  target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64)
 else()
   message(STATUS "Building AMDGPU plugin for dlopened libhsa")
   target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
   target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
-  target_link_libraries(omptarget.rtl.amdgpu PRIVATE LLVMFrontendOffloading)
 endif()
 
 # Configure testing for the AMDGPU plugin. We will build tests if we could a
diff --git a/offload/plugins-nextgen/common/CMakeLists.txt b/offload/plugins-nextgen/common/CMakeLists.txt
index 3a861a47eedabc..155500056307a8 100644
--- a/offload/plugins-nextgen/common/CMakeLists.txt
+++ b/offload/plugins-nextgen/common/CMakeLists.txt
@@ -1,13 +1,16 @@
 # NOTE: Don't try to build `PluginInterface` using `add_llvm_library` because we
 # don't want to export `PluginInterface` while `add_llvm_library` requires that.
-add_library(PluginCommon OBJECT
+add_library(PluginCommon STATIC
   src/PluginInterface.cpp
   src/GlobalHandler.cpp
   src/JIT.cpp
   src/RPC.cpp
   src/Utils/ELF.cpp
 )
-add_dependencies(PluginCommon intrinsics_gen LLVMProfileData)
+add_dependencies(PluginCommon intrinsics_gen)
+if (TARGET LLVMProfileData)
+  add_dependencies(PluginCommon LLVMProfileData)
+endif()
 
 # Only enable JIT for those targets that LLVM can support.
 set(supported_jit_targets AMDGPU NVPTX)
@@ -43,7 +46,11 @@ target_compile_definitions(PluginCommon PRIVATE
 
 target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
 target_link_options(PluginCommon PUBLIC ${offload_link_flags})
-target_link_libraries(PluginCommon PRIVATE LLVMProfileData)
+set(USE_SHARED)
+if (LLVM_LINK_LLVM_DYLIB)
+  set(USE_SHARED USE_SHARED)
+endif()
+llvm_config(PluginCommon ${USE_SHARED} ProfileData)
 
 target_include_directories(PluginCommon PUBLIC
   ${CMAKE_CURRENT_SOURCE_DIR}/include

>From 76caeadfedf3a82d28e9b334bb5009a4b0fc6a7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:30:54 +0100
Subject: [PATCH 3/8] [offload] Copy cmake/DetectTestCompiler from openmp

Copy DetectTestCompiler directory as required
by `cmake/OpenMPTesting.cmake`.
---
 .../cmake/DetectTestCompiler/CMakeLists.txt   | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 offload/cmake/DetectTestCompiler/CMakeLists.txt

diff --git a/offload/cmake/DetectTestCompiler/CMakeLists.txt b/offload/cmake/DetectTestCompiler/CMakeLists.txt
new file mode 100644
index 00000000000000..8ea7ab8d45ba10
--- /dev/null
+++ b/offload/cmake/DetectTestCompiler/CMakeLists.txt
@@ -0,0 +1,47 @@
+cmake_minimum_required(VERSION 3.20.0)
+project(DetectTestCompiler C CXX)
+
+include(CheckCCompilerFlag)
+include(CheckCXXCompilerFlag)
+include(CheckIncludeFile)
+include(CheckIncludeFileCXX)
+
+function(write_compiler_information lang)
+  set(information "${CMAKE_${lang}_COMPILER}")
+  set(information "${information}\\;${CMAKE_${lang}_COMPILER_ID}")
+  set(information "${information}\\;${CMAKE_${lang}_COMPILER_VERSION}")
+  set(information "${information}\\;${${lang}_FLAGS}")
+  set(information "${information}\\;${${lang}_HAS_TSAN_FLAG}")
+  set(information "${information}\\;${${lang}_HAS_OMIT_FRAME_POINTER}")
+  set(information "${information}\\;${${lang}_HAS_OMP_H}")
+  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lang}CompilerInformation.txt ${information})
+endfunction(write_compiler_information)
+
+find_package(OpenMP)
+if (NOT OpenMP_Found)
+  set(OpenMP_C_FLAGS "-fopenmp")
+  set(OpenMP_CXX_FLAGS "-fopenmp")
+endif()
+
+set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
+set(THREADS_PREFER_PTHREAD_FLAG TRUE)
+find_package(Threads REQUIRED)
+
+set(C_FLAGS "${OpenMP_C_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
+set(CXX_FLAGS "${OpenMP_CXX_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
+
+check_c_compiler_flag("-fno-omit-frame-pointer" C_HAS_OMIT_FRAME_POINTER)
+check_cxx_compiler_flag("-fno-omit-frame-pointer" CXX_HAS_OMIT_FRAME_POINTER)
+
+set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
+set(CMAKE_REQUIRED_FLAGS "-fsanitize=thread")
+check_c_compiler_flag("" C_HAS_TSAN_FLAG)
+check_cxx_compiler_flag("" CXX_HAS_TSAN_FLAG)
+set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
+
+# Check if omp.h header exists for the test compiler
+check_include_file_cxx(omp.h CXX_HAS_OMP_H)
+check_include_file(omp.h C_HAS_OMP_H)
+
+write_compiler_information(C)
+write_compiler_information(CXX)

>From fbd16b9c133dd30ecf20dddf060b33e45b4dcc0e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:32:39 +0100
Subject: [PATCH 4/8] [offload] [test] Add "omp" test dependency only when
 present

Add the `omp` target dependency to tests only when the respective target
is present, i.e. when not building standalone against system libomp.
---
 offload/test/CMakeLists.txt | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/offload/test/CMakeLists.txt b/offload/test/CMakeLists.txt
index 5a6f637b57fa7b..f8bb7b60475d65 100644
--- a/offload/test/CMakeLists.txt
+++ b/offload/test/CMakeLists.txt
@@ -28,6 +28,11 @@ if(CUDAToolkit_FOUND)
   get_filename_component(CUDA_LIBDIR "${CUDA_cudart_static_LIBRARY}" DIRECTORY)
 endif()
 
+set(OMP_DEPEND)
+if(TARGET omp)
+  set(OMP_DEPEND omp)
+endif()
+
 string(REGEX MATCHALL "([^\ ]+\ |[^\ ]+$)" SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS}")
 foreach(CURRENT_TARGET IN LISTS SYSTEM_TARGETS)
   string(STRIP "${CURRENT_TARGET}" CURRENT_TARGET)
@@ -35,7 +40,7 @@ foreach(CURRENT_TARGET IN LISTS SYSTEM_TARGETS)
   add_offload_testsuite(check-libomptarget-${CURRENT_TARGET}
     "Running libomptarget tests"
     ${CMAKE_CURRENT_BINARY_DIR}/${CURRENT_TARGET}
-    DEPENDS omptarget omp ${LIBOMPTARGET_TESTED_PLUGINS}
+    DEPENDS omptarget ${OMP_DEPEND} ${LIBOMPTARGET_TESTED_PLUGINS}
     ARGS ${LIBOMPTARGET_LIT_ARG_LIST})
   list(APPEND LIBOMPTARGET_LIT_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CURRENT_TARGET})
 
@@ -49,12 +54,12 @@ add_offload_testsuite(check-libomptarget
   "Running libomptarget tests"
   ${LIBOMPTARGET_LIT_TESTSUITES}
   EXCLUDE_FROM_CHECK_ALL
-  DEPENDS omptarget omp ${LIBOMPTARGET_TESTED_PLUGINS}
+  DEPENDS omptarget ${OMP_DEPEND} ${LIBOMPTARGET_TESTED_PLUGINS}
   ARGS ${LIBOMPTARGET_LIT_ARG_LIST})
 
 add_offload_testsuite(check-offload
   "Running libomptarget tests"
   ${LIBOMPTARGET_LIT_TESTSUITES}
   EXCLUDE_FROM_CHECK_ALL
-  DEPENDS omptarget omp ${LIBOMPTARGET_TESTED_PLUGINS}
+  DEPENDS omptarget ${OMP_DEPEND} ${LIBOMPTARGET_TESTED_PLUGINS}
   ARGS ${LIBOMPTARGET_LIT_ARG_LIST})

>From 4b92fb7b0099d1f5be532d141a40c66b801b434f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:39:53 +0100
Subject: [PATCH 5/8] [offload] Define OPENMP_TEST*_FLAGS in standalone builds

Copy the `OPENMP_TEST_FLAGS` and `OPENMP_TEST_OPENMP_FLAGS` from openmp
for standalone builds, as required by the lit configs.
---
 offload/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index b464de16e8bc6d..9dd988713de1f8 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -287,6 +287,11 @@ if(OPENMP_STANDALONE_BUILD)
       ${LLVM_LIBRARY_DIRS}
     REQUIRED
   )
+
+  set(OPENMP_TEST_FLAGS "" CACHE STRING
+    "Extra compiler flags to send to the test compiler.")
+  set(OPENMP_TEST_OPENMP_FLAGS ${OPENMP_TEST_COMPILER_OPENMP_FLAGS} CACHE STRING
+    "OpenMP compiler flag to use for testing OpenMP runtime libraries.")
 endif()
 
 macro(pythonize_bool var)

>From 0d39fbd2c8c2a5cdc65e02b4ec4a523bdaaff481 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:43:13 +0100
Subject: [PATCH 6/8] [offload] Move LIBOMPTARGET_OPENMP_*_FOLDER from openmp

Move `LIBOMPTARGET_OPENMP_HEADER_FOLDER`
and `LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER` definitions from openmp
to offload, as they are only used in the latter project.
---
 offload/CMakeLists.txt            | 6 ++++++
 openmp/runtime/src/CMakeLists.txt | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index 9dd988713de1f8..849bde2890e58a 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -351,6 +351,12 @@ set(LIBOMPTARGET_LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE STRING
 set(LIBOMPTARGET_LLVM_LIBRARY_INTDIR "${LIBOMPTARGET_INTDIR}" CACHE STRING
   "Path to folder where intermediate libraries will be output")
 
+# Definitions for testing, for reuse when testing libomptarget-nvptx.
+set(LIBOMPTARGET_OPENMP_HEADER_FOLDER "${LIBOMP_INCLUDE_DIR}" CACHE STRING
+  "Path to folder containing omp.h")
+set(LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER "${LIBOMP_LIBRARY_DIR}" CACHE STRING
+  "Path to folder containing libomp.so, and libLLVMSupport.so with profiling enabled")
+
 # Build offloading plugins and device RTLs if they are available.
 add_subdirectory(plugins-nextgen)
 add_subdirectory(DeviceRTL)
diff --git a/openmp/runtime/src/CMakeLists.txt b/openmp/runtime/src/CMakeLists.txt
index 698e185d9c4dde..97268ae37c9ae8 100644
--- a/openmp/runtime/src/CMakeLists.txt
+++ b/openmp/runtime/src/CMakeLists.txt
@@ -272,12 +272,6 @@ if(NOT WIN32)
   endif()
 endif()
 
-# Definitions for testing, for reuse when testing libomptarget-nvptx.
-set(LIBOMPTARGET_OPENMP_HEADER_FOLDER "${LIBOMP_INCLUDE_DIR}" CACHE STRING
-  "Path to folder containing omp.h")
-set(LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER "${LIBOMP_LIBRARY_DIR}" CACHE STRING
-  "Path to folder containing libomp.so, and libLLVMSupport.so with profiling enabled")
-
 # Create *.inc before compiling any sources
 # objects depend on : .inc files
 add_custom_target(libomp-needed-headers DEPENDS kmp_i18n_id.inc kmp_i18n_default.inc)

>From 28a867c7db64972b6bf7a75b5601014bd614ff47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:44:37 +0100
Subject: [PATCH 7/8] [offload] Define LIBOMP_*_DIR in standalone builds

Define `LIBOMP_INCLUDE_DIR` and `LIBOMP_LIBRARY_DIR` for testing
in standalone builds.
---
 offload/CMakeLists.txt | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index 849bde2890e58a..bc7d1f8fa5710d 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -288,6 +288,17 @@ if(OPENMP_STANDALONE_BUILD)
     REQUIRED
   )
 
+  find_path (
+    LIBOMP_INCLUDE_DIR
+    NAMES
+      omp.h
+    HINTS
+    ${COMPILER_RESOURCE_DIR}/include
+    ${CMAKE_INSTALL_PREFIX}/include
+  )
+
+  get_filename_component(LIBOMP_LIBRARY_DIR ${LIBOMP_STANDALONE} DIRECTORY)
+
   set(OPENMP_TEST_FLAGS "" CACHE STRING
     "Extra compiler flags to send to the test compiler.")
   set(OPENMP_TEST_OPENMP_FLAGS ${OPENMP_TEST_COMPILER_OPENMP_FLAGS} CACHE STRING

>From 1638053765b26bc61c1a77e49a47d4e396b17a87 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Sat, 30 Nov 2024 15:53:36 +0100
Subject: [PATCH 8/8] [offload] Fix path to llvm-offload-device-info in
 standalone build

Fix path to `llvm-offload-device-info` tool in standalone build.
This tool is built as part of offload, so rather than looking it up
as a preinstalled tool, set correct `LLVM_RUNTIME_OUTPUT_INTDIR`
for the `llvm_add_tool()` call, and unconditionally set
`OFFLOAD_DEVICE_INFO_EXECUTABLE` using that directory.
---
 offload/CMakeLists.txt            |  3 +++
 offload/cmake/OpenMPTesting.cmake | 13 +------------
 2 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index bc7d1f8fa5710d..d6312fa6fb7563 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -64,6 +64,9 @@ if (OPENMP_STANDALONE_BUILD)
   # Do not use OPENMP_LIBDIR_SUFFIX directly, use OPENMP_INSTALL_LIBDIR.
   set(OPENMP_INSTALL_LIBDIR "lib${OPENMP_LIBDIR_SUFFIX}")
 
+  # Used by llvm_add_tool() and tests.
+  set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR})
+
   # Group test settings.
   set(OPENMP_TEST_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING
     "C compiler to use for testing OpenMP runtime libraries.")
diff --git a/offload/cmake/OpenMPTesting.cmake b/offload/cmake/OpenMPTesting.cmake
index 6609d6301d0f93..567848a531bbc9 100644
--- a/offload/cmake/OpenMPTesting.cmake
+++ b/offload/cmake/OpenMPTesting.cmake
@@ -37,17 +37,6 @@ function(find_standalone_test_dependencies)
     return()
   endif()
 
-  find_program(OFFLOAD_DEVICE_INFO_EXECUTABLE
-    NAMES llvm-offload-device-info
-    PATHS ${OPENMP_LLVM_TOOLS_DIR})
-  if (NOT OFFLOAD_DEVICE_INFO_EXECUTABLE)
-    message(STATUS "Cannot find 'llvm-offload-device-info'.")
-    message(STATUS "Please put 'not' in your PATH, set OFFLOAD_DEVICE_INFO_EXECUTABLE to its full path, or point OPENMP_LLVM_TOOLS_DIR to its directory.")
-    message(WARNING "The check targets will not be available!")
-    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
-    return()
-  endif()
-
   find_program(OPENMP_NOT_EXECUTABLE
     NAMES not
     PATHS ${OPENMP_LLVM_TOOLS_DIR})
@@ -82,8 +71,8 @@ else()
     set(OPENMP_FILECHECK_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/FileCheck)
   endif()
   set(OPENMP_NOT_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/not)
-  set(OFFLOAD_DEVICE_INFO_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-offload-device-info)
 endif()
+set(OFFLOAD_DEVICE_INFO_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-offload-device-info)
 
 # Macro to extract information about compiler from file. (no own scope)
 macro(extract_test_compiler_information lang file)



More information about the Openmp-commits mailing list