Skip to content

Commit f2f88f3

Browse files
author
Vyacheslav Zakharin
committed
An attempt to abandon omptarget out-of-tree builds.
I want to start using LLVM component libraries in libomptarget to stop duplicating implementations already available in LLVM (e.g. LLVMObject, LLVMSupport, etc.). Without relying on LLVM in all libomptarget builds one has to provide fallback implementation for each used LLVM feature. This is an attempt to stop supporting out-of-llvm-tree builds of libomptarget. I understand that I may need to revert this, if this affects downstream projects in a bad way. Differential Revision: https://reviews.llvm.org/D101509
1 parent 3444996 commit f2f88f3

File tree

5 files changed

+57
-91
lines changed

5 files changed

+57
-91
lines changed

openmp/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ else()
3939
set(OPENMP_TEST_C_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
4040
set(OPENMP_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
4141
endif()
42-
43-
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include)
4442
endif()
4543

4644
# Check and set up common compiler flags.
@@ -63,20 +61,6 @@ if (APPLE OR WIN32 OR NOT OPENMP_HAVE_STD_CPP14_FLAG)
6361
set(ENABLE_LIBOMPTARGET OFF)
6462
endif()
6563

66-
# Attempt to locate LLVM source, required by libomptarget
67-
if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
68-
if (LLVM_MAIN_INCLUDE_DIR)
69-
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_MAIN_INCLUDE_DIR})
70-
elseif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../llvm/include)
71-
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../llvm/include)
72-
endif()
73-
endif()
74-
75-
if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
76-
message(STATUS "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS, disabling libomptarget")
77-
set(ENABLE_LIBOMPTARGET OFF)
78-
endif()
79-
8064
option(OPENMP_ENABLE_LIBOMPTARGET "Enable building libomptarget for offloading."
8165
${ENABLE_LIBOMPTARGET})
8266
option(OPENMP_ENABLE_LIBOMPTARGET_PROFILING "Enable time profiling for libomptarget."

openmp/README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ These flags are **appended**, they do not overwrite any of the preset flags.
243243
Options for ``libomptarget``
244244
----------------------------
245245

246+
An installed LLVM package is a prerequisite for building ``libomptarget``
247+
library. So ``libomptarget`` may only be built in two cases:
248+
249+
- As a project of a regular LLVM build via **LLVM_ENABLE_PROJECTS**,
250+
**LLVM_EXTERNAL_PROJECTS**, or **LLVM_ENABLE_RUNTIMES** or
251+
- as a standalone project build that uses a pre-installed LLVM package.
252+
In this mode one has to make sure that the default CMake
253+
``find_package(LLVM)`` call `succeeds <https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure>`_.
254+
246255
**LIBOMPTARGET_OPENMP_HEADER_FOLDER** = ``""``
247256
Path of the folder that contains ``omp.h``. This is required for testing
248257
out-of-tree builds.

openmp/libomptarget/cmake/Modules/LibomptargetGetDependencies.cmake

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@
1919

2020
include (FindPackageHandleStandardArgs)
2121

22+
################################################################################
23+
# Looking for LLVM...
24+
################################################################################
25+
26+
if (OPENMP_STANDALONE_BUILD)
27+
# Complete LLVM package is required for building libomptarget
28+
# in an out-of-tree mode.
29+
find_package(LLVM REQUIRED)
30+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
31+
message(STATUS "Using LLVM in: ${LLVM_DIR}")
32+
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
33+
list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
34+
include(AddLLVM)
35+
else()
36+
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS
37+
${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include
38+
)
39+
endif()
40+
2241
################################################################################
2342
# Looking for libelf...
2443
################################################################################
@@ -250,27 +269,4 @@ if (NOT LIBOMPTARGET_CUDA_TOOLKIT_ROOT_DIR_PRESET AND
250269
endif()
251270
endif()
252271

253-
if (OPENMP_STANDALONE_BUILD)
254-
# This duplicates code from llvm/cmake/config-ix.cmake
255-
if( WIN32 AND NOT CYGWIN )
256-
# We consider Cygwin as another Unix
257-
set(PURE_WINDOWS 1)
258-
endif()
259-
260-
# library checks
261-
if( NOT PURE_WINDOWS )
262-
check_library_exists(pthread pthread_create "" HAVE_LIBPTHREAD)
263-
endif()
264-
265-
if(HAVE_LIBPTHREAD)
266-
# We want to find pthreads library and at the moment we do want to
267-
# have it reported as '-l<lib>' instead of '-pthread'.
268-
# TODO: switch to -pthread once the rest of the build system can deal with it.
269-
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
270-
set(THREADS_HAVE_PTHREAD_ARG Off)
271-
find_package(Threads REQUIRED)
272-
set(OPENMP_PTHREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
273-
endif()
274-
else()
275-
set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})
276-
endif()
272+
set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})

openmp/libomptarget/deviceRTLs/amdgcn/CMakeLists.txt

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,6 @@
1010
#
1111
##===----------------------------------------------------------------------===##
1212

13-
find_package(LLVM QUIET CONFIG
14-
PATHS
15-
$ENV{AOMP}
16-
$ENV{HOME}/rocm/aomp
17-
/opt/rocm/aomp
18-
/usr/lib/rocm/aomp
19-
${LIBOMPTARGET_NVPTX_CUDA_COMPILER_DIR}
20-
${LIBOMPTARGET_NVPTX_CUDA_LINKER_DIR}
21-
${CMAKE_CXX_COMPILER_DIR}
22-
NO_DEFAULT_PATH)
23-
24-
if (LLVM_DIR)
25-
libomptarget_say("Found LLVM ${LLVM_PACKAGE_VERSION}. Configure: ${LLVM_DIR}/LLVMConfig.cmake")
26-
else()
27-
libomptarget_say("Not building AMDGCN device RTL: AOMP not found")
28-
return()
29-
endif()
30-
31-
set(AOMP_INSTALL_PREFIX ${LLVM_INSTALL_PREFIX})
32-
33-
if (AOMP_INSTALL_PREFIX)
34-
set(AOMP_BINDIR ${AOMP_INSTALL_PREFIX}/bin)
35-
else()
36-
set(AOMP_BINDIR ${LLVM_BUILD_BINARY_DIR}/bin)
37-
endif()
38-
3913
# Copied from nvptx CMakeLists
4014
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
4115
set(aux_triple x86_64-unknown-linux-gnu)
@@ -48,7 +22,20 @@ else()
4822
return()
4923
endif()
5024

51-
libomptarget_say("Building AMDGCN device RTL. LLVM_COMPILER_PATH=${AOMP_BINDIR}")
25+
if (LLVM_DIR)
26+
# Builds that use pre-installed LLVM have LLVM_DIR set.
27+
find_program(CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
28+
find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR}
29+
NO_DEFAULT_PATH)
30+
find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
31+
libomptarget_say("Building AMDGCN device RTL. Using clang: ${CLANG_TOOL}")
32+
else()
33+
# LLVM in-tree builds may use CMake target names to discover the tools.
34+
set(CLANG_TOOL $<TARGET_FILE:clang>)
35+
set(LINK_TOOL $<TARGET_FILE:llvm-link>)
36+
set(OPT_TOOL $<TARGET_FILE:opt>)
37+
libomptarget_say("Building AMDGCN device RTL. Using clang from in-tree build")
38+
endif()
5239

5340
project(omptarget-amdgcn)
5441

@@ -109,7 +96,7 @@ if (DEFINED LIBOMPTARGET_AMDGCN_GFXLIST)
10996
endif()
11097

11198
macro(add_cuda_bc_library)
112-
set(cu_cmd ${AOMP_BINDIR}/clang++
99+
set(cu_cmd ${CLANG_TOOL}
113100
-xc++
114101
-c
115102
-std=c++14
@@ -145,7 +132,7 @@ macro(add_cuda_bc_library)
145132

146133
add_custom_command(
147134
OUTPUT linkout.cuda.${mcpu}.bc
148-
COMMAND ${AOMP_BINDIR}/llvm-link ${bc1_files} -o linkout.cuda.${mcpu}.bc
135+
COMMAND ${LINK_TOOL} ${bc1_files} -o linkout.cuda.${mcpu}.bc
149136
DEPENDS ${bc1_files})
150137

151138
list(APPEND bc_files linkout.cuda.${mcpu}.bc)
@@ -160,7 +147,7 @@ foreach(mcpu ${mcpus})
160147
set(bc_libname lib${libname}-${mcpu}.bc)
161148
add_custom_command(
162149
OUTPUT ${bc_libname}
163-
COMMAND ${AOMP_BINDIR}/llvm-link ${bc_files} | ${AOMP_BINDIR}/opt --always-inline -o ${OUTPUTDIR}/${bc_libname}
150+
COMMAND ${LINK_TOOL} ${bc_files} | ${OPT_TOOL} --always-inline -o ${OUTPUTDIR}/${bc_libname}
164151
DEPENDS ${bc_files})
165152

166153
add_custom_target(lib${libname}-${mcpu} ALL DEPENDS ${bc_libname})

openmp/libomptarget/src/CMakeLists.txt

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,19 @@ set(LIBOMPTARGET_SRC_FILES ${LIBOMPTARGET_SRC_FILES} PARENT_SCOPE)
2424

2525
include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})
2626

27-
# Build libomptarget library with libdl dependency. Add LLVMSupport
28-
# dependency if building in-tree with profiling enabled.
29-
if(OPENMP_STANDALONE_BUILD OR (NOT OPENMP_ENABLE_LIBOMPTARGET_PROFILING))
30-
add_library(omptarget SHARED ${LIBOMPTARGET_SRC_FILES})
31-
target_link_libraries(omptarget
32-
${CMAKE_DL_LIBS}
33-
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports")
34-
else()
35-
set(LLVM_LINK_COMPONENTS
36-
Support
37-
)
38-
add_llvm_library(omptarget SHARED ${LIBOMPTARGET_SRC_FILES}
39-
LINK_LIBS ${CMAKE_DL_LIBS}
40-
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports"
41-
)
27+
# Build libomptarget library with libdl dependency.
28+
add_library(omptarget SHARED ${LIBOMPTARGET_SRC_FILES})
29+
if (OPENMP_ENABLE_LIBOMPTARGET_PROFILING)
30+
# Add LLVMSupport dependency if profiling is enabled.
31+
# Linking with LLVM component libraries also requires
32+
# aligning the compile flags.
33+
llvm_update_compile_flags(omptarget)
4234
target_compile_definitions(omptarget PUBLIC OMPTARGET_PROFILE_ENABLED)
35+
target_link_libraries(omptarget PRIVATE LLVMSupport)
4336
endif()
44-
45-
# libomptarget needs to be set separately because add_llvm_library doesn't
46-
# conform with location configuration of its parent scope.
47-
set_target_properties(omptarget
48-
PROPERTIES
49-
LIBRARY_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})
37+
target_link_libraries(omptarget PRIVATE
38+
${CMAKE_DL_LIBS}
39+
"-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports")
5040

5141
# Install libomptarget under the lib destination folder.
5242
install(TARGETS omptarget LIBRARY COMPONENT omptarget

0 commit comments

Comments
 (0)