Skip to content

Commit d6b09d3

Browse files
committed
[libc] Build the GPU during the projects setup like libc-hdrgen
Summary: The libc build has a few utilties that need to be built before we can do everything in the full build. The one requirement currently is the `libc-hdrgen` binary. If we are doing a full build runtimes mode we first add `libc` to the projects list and then only use the `projects` portion to buld the `libc` portion. We also use utilities for the GPU build, namely the loader utilities. Previously we would build these tools on-demand inside of the cross-build, which tool some hacky workarounds for the dependency finding and target triple. This patch instead just builds them similarly to libc-hdrgen and then passses them in. We now either pass it manually it it was built, or just look it up like we do with the other `clang` tools. Depends on #84664
1 parent 0c155cf commit d6b09d3

File tree

10 files changed

+65
-61
lines changed

10 files changed

+65
-61
lines changed

libc/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ if(LLVM_LIBC_FULL_BUILD OR LLVM_LIBC_GPU_BUILD)
6060
message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
6161
endif()
6262
endif()
63+
# We will build the GPU utilities if we are not doing a runtimes build.
64+
if(LLVM_LIBC_GPU_BUILD AND NOT LLVM_RUNTIMES_BUILD)
65+
add_subdirectory(utils/gpu)
66+
endif()
6367

6468
set(NEED_LIBC_HDRGEN FALSE)
6569
if(NOT LLVM_RUNTIMES_BUILD)
@@ -79,11 +83,6 @@ if(LIBC_HDRGEN_ONLY OR NEED_LIBC_HDRGEN)
7983
# When libc is build as part of the runtimes/bootstrap build's CMake run, we
8084
# only need to build the host tools to build the libc. So, we just do enough
8185
# to build libc-hdrgen and return.
82-
83-
# Always make the RPC server availible to other projects for GPU mode.
84-
if(LLVM_LIBC_GPU_BUILD)
85-
add_subdirectory(utils/gpu/server)
86-
endif()
8786
return()
8887
endif()
8988
unset(NEED_LIBC_HDRGEN)

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ else()
9393
endif()
9494
set(LIBC_GPU_TARGET_ARCHITECTURE "${gpu_test_architecture}")
9595

96+
# Identify the GPU loader utility used to run tests.
97+
set(LIBC_GPU_LOADER_EXECUTABLE "" CACHE STRING "Executable for the GPU loader.")
98+
if(LIBC_GPU_LOADER_EXECUTABLE)
99+
set(gpu_loader_executable ${LIBC_GPU_LOADER_EXECUTABLE})
100+
elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
101+
find_program(LIBC_AMDHSA_LOADER_EXECUTABLE
102+
NAMES amdhsa-loader NO_DEFAULT_PATH
103+
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
104+
if(LIBC_AMDHSA_LOADER_EXECUTABLE)
105+
set(gpu_loader_executable ${LIBC_AMDHSA_LOADER_EXECUTABLE})
106+
endif()
107+
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
108+
find_program(LIBC_NVPTX_LOADER_EXECUTABLE
109+
NAMES nvptx-loader NO_DEFAULT_PATH
110+
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
111+
if(LIBC_NVPTX_LOADER_EXECUTABLE)
112+
set(gpu_loader_executable ${LIBC_NVPTX_LOADER_EXECUTABLE})
113+
endif()
114+
endif()
115+
if(NOT TARGET libc.utils.gpu.loader AND gpu_loader_executable)
116+
add_custom_target(libc.utils.gpu.loader)
117+
set_target_properties(
118+
libc.utils.gpu.loader
119+
PROPERTIES
120+
EXECUTABLE "${gpu_loader_executable}"
121+
)
122+
endif()
123+
124+
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
125+
# The AMDGPU environment uses different code objects to encode the ABI for
126+
# kernel calls and intrinsic functions. We want to specify this manually to
127+
# conform to whatever the test suite was built to handle.
128+
set(LIBC_GPU_CODE_OBJECT_VERSION 5)
129+
endif()
130+
96131
if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
97132
# FIXME: This is a hack required to keep the CUDA package from trying to find
98133
# pthreads. We only link the CUDA driver, so this is unneeded.
@@ -103,10 +138,3 @@ if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
103138
get_filename_component(LIBC_CUDA_ROOT "${CUDAToolkit_BIN_DIR}" DIRECTORY ABSOLUTE)
104139
endif()
105140
endif()
106-
107-
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
108-
# The AMDGPU environment uses different code objects to encode the ABI for
109-
# kernel calls and intrinsic functions. We want to specify this manually to
110-
# conform to whatever the test suite was built to handle.
111-
set(LIBC_GPU_CODE_OBJECT_VERSION 5)
112-
endif()

libc/utils/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
if(LLVM_INCLUDE_TESTS)
22
add_subdirectory(MPFRWrapper)
33
endif()
4-
if(LIBC_TARGET_OS_IS_GPU)
5-
add_subdirectory(gpu)
6-
endif()

libc/utils/gpu/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
add_subdirectory(server)
2-
if(LIBC_TARGET_OS_IS_GPU)
3-
add_subdirectory(loader)
4-
endif()
2+
add_subdirectory(loader)

libc/utils/gpu/loader/CMakeLists.txt

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,18 @@ target_include_directories(gpu_loader PUBLIC
66
${LIBC_SOURCE_DIR}
77
)
88

9-
# This utility needs to be compiled for the host system when cross compiling.
10-
if(LLVM_RUNTIMES_TARGET OR LIBC_TARGET_TRIPLE)
11-
target_compile_options(gpu_loader PUBLIC --target=${LLVM_HOST_TRIPLE})
12-
target_link_libraries(gpu_loader PUBLIC "--target=${LLVM_HOST_TRIPLE}")
13-
endif()
14-
159
find_package(hsa-runtime64 QUIET 1.2.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
16-
if(hsa-runtime64_FOUND AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
10+
if(hsa-runtime64_FOUND)
1711
add_subdirectory(amdgpu)
18-
elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
19-
message(STATUS "Skipping HSA loader for gpu target, no HSA was detected")
2012
endif()
2113

2214
# The CUDA loader requires LLVM to traverse the ELF image for symbols.
23-
find_package(LLVM QUIET)
24-
if(CUDAToolkit_FOUND AND LLVM_FOUND AND LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
15+
find_package(CUDAToolkit 11.2 QUIET)
16+
if(CUDAToolkit_FOUND)
2517
add_subdirectory(nvptx)
26-
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
27-
message(STATUS "Skipping CUDA loader for gpu target, no CUDA was detected")
2818
endif()
2919

30-
# Add a custom target to be used for testing.
31-
set(LIBC_GPU_LOADER_EXECUTABLE "" CACHE STRING "Overriding binary for the GPU loader.")
32-
if(LIBC_GPU_LOADER_EXECUTABLE)
33-
add_custom_target(libc.utils.gpu.loader)
34-
set_target_properties(
35-
libc.utils.gpu.loader
36-
PROPERTIES
37-
EXECUTABLE "${LIBC_GPU_LOADER_EXECUTABLE}"
38-
)
39-
elseif(TARGET amdhsa-loader AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
20+
if(TARGET amdhsa-loader AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
4021
add_custom_target(libc.utils.gpu.loader)
4122
add_dependencies(libc.utils.gpu.loader amdhsa-loader)
4223
set_target_properties(
@@ -56,11 +37,10 @@ elseif(TARGET nvptx-loader AND LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
5637
)
5738
endif()
5839

59-
if(TARGET libc.utils.gpu.loader)
60-
get_target_property(gpu_loader_tgt libc.utils.gpu.loader "TARGET")
40+
foreach(gpu_loader_tgt amdhsa-loader nvptx-loader)
6141
if(gpu_loader_tgt)
6242
install(TARGETS ${gpu_loader_tgt}
6343
DESTINATION ${CMAKE_INSTALL_BINDIR}
6444
COMPONENT libc)
6545
endif()
66-
endif()
46+
endforeach()

libc/utils/gpu/loader/amdgpu/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
add_executable(amdhsa-loader Loader.cpp)
2-
add_dependencies(amdhsa-loader libc.src.__support.RPC.rpc)
32

43
target_link_libraries(amdhsa-loader
54
PRIVATE

libc/utils/gpu/loader/nvptx/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
add_executable(nvptx-loader Loader.cpp)
2-
add_dependencies(nvptx-loader libc.src.__support.RPC.rpc)
32

43
if(NOT LLVM_ENABLE_RTTI)
54
target_compile_options(nvptx-loader PRIVATE -fno-rtti)
65
endif()
7-
target_include_directories(nvptx-loader PRIVATE ${LLVM_INCLUDE_DIRS})
6+
target_include_directories(nvptx-loader PRIVATE
7+
${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include)
88
target_link_libraries(nvptx-loader
99
PRIVATE
1010
gpu_loader

libc/utils/gpu/server/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@ target_include_directories(llvmlibc_rpc_server PRIVATE ${LIBC_SOURCE_DIR})
55
target_include_directories(llvmlibc_rpc_server PUBLIC ${LIBC_SOURCE_DIR}/include)
66
target_include_directories(llvmlibc_rpc_server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
77

8-
98
# Ignore unsupported clang attributes if we're using GCC.
109
target_compile_options(llvmlibc_rpc_server PUBLIC
1110
$<$<CXX_COMPILER_ID:GNU>:-Wno-attributes>)
1211
target_compile_definitions(llvmlibc_rpc_server PUBLIC
1312
LIBC_NAMESPACE=${LIBC_NAMESPACE})
1413

15-
# This utility needs to be compiled for the host system when cross compiling.
16-
if(LLVM_RUNTIMES_TARGET OR LIBC_TARGET_TRIPLE)
17-
target_compile_options(llvmlibc_rpc_server PUBLIC
18-
--target=${LLVM_HOST_TRIPLE})
19-
target_link_libraries(llvmlibc_rpc_server PUBLIC
20-
"--target=${LLVM_HOST_TRIPLE}")
21-
endif()
22-
2314
# Install the server and associated header.
2415
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/llvmlibc_rpc_server.h
2516
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}

llvm/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ foreach(_name ${LLVM_RUNTIME_TARGETS})
182182
endif()
183183
endif()
184184
endforeach()
185+
if("${LIBC_TARGET_TRIPLE}" STREQUAL "amdgcn-amd-amdhsa" OR
186+
"${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda")
187+
set(LLVM_LIBC_GPU_BUILD ON)
188+
endif()
185189
if(NEED_LIBC_HDRGEN)
186190
# To build the libc runtime, we need to be able to build few libc build
187191
# tools from the "libc" project. So, we add it to the list of enabled

llvm/runtimes/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,28 @@ if(runtimes)
452452
if(LLVM_LIBC_GPU_BUILD)
453453
list(APPEND libc_cmake_args "-DLLVM_LIBC_GPU_BUILD=ON")
454454
if("libc" IN_LIST RUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES)
455+
if(TARGET amdhsa-loader)
456+
list(APPEND libc_cmake_args
457+
"-DRUNTIMES_amdgcn-amd-amdhsa_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:amdhsa-loader>")
458+
list(APPEND extra_deps amdhsa-loader amdgpu-arch)
459+
endif()
455460
list(APPEND libc_cmake_args "-DRUNTIMES_amdgcn-amd-amdhsa_LLVM_LIBC_FULL_BUILD=ON")
456461
endif()
457462
if("libc" IN_LIST RUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES)
463+
if(TARGET nvptx-loader)
464+
list(APPEND libc_cmake_args
465+
"-DRUNTIMES_nvptx64-nvidia-cuda_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:nvptx-loader>")
466+
list(APPEND extra_deps nvptx-loader nvptx-arch)
467+
endif()
458468
list(APPEND libc_cmake_args "-DRUNTIMES_nvptx64-nvidia-cuda_LLVM_LIBC_FULL_BUILD=ON")
459469
endif()
460470
# The `libc` project may require '-DCUDAToolkit_ROOT' in GPU mode.
461471
if(CUDAToolkit_ROOT)
462472
list(APPEND libc_cmake_args "-DCUDAToolkit_ROOT=${CUDAToolkit_ROOT}")
463473
endif()
464-
foreach(dep clang-offload-packager nvptx-arch amdgpu-arch)
465-
if(TARGET ${dep})
466-
list(APPEND extra_deps ${dep})
467-
endif()
468-
endforeach()
474+
if(TARGET clang-offload-packager)
475+
list(APPEND extra_deps clang-offload-packager)
476+
endif()
469477
endif()
470478
if(LLVM_LIBC_FULL_BUILD)
471479
list(APPEND libc_cmake_args "-DLLVM_LIBC_FULL_BUILD=ON")

0 commit comments

Comments
 (0)