Skip to content

Commit df2a567

Browse files
committed
Revert "[LLVM] Make the GPU loader utilities an LLVM tool (#132096)"
This reverts commit 221b011. Some build failures requiring TargetParser and some warnings to clean up.
1 parent 00fabd2 commit df2a567

File tree

12 files changed

+117
-102
lines changed

12 files changed

+117
-102
lines changed

libc/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ set(LIBC_NAMESPACE ${default_namespace}
5959
CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
6060
)
6161

62+
# We will build the GPU utilities if we are not doing a runtimes build.
63+
option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF)
64+
if(LIBC_BUILD_GPU_LOADER OR ((NOT LLVM_RUNTIMES_BUILD) AND LLVM_LIBC_GPU_BUILD))
65+
add_subdirectory(utils/gpu)
66+
return()
67+
endif()
68+
6269
option(LIBC_CMAKE_VERBOSE_LOGGING
6370
"Log details warnings and notifications during CMake configuration." OFF)
6471

libc/utils/gpu/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(loader)

libc/utils/gpu/loader/CMakeLists.txt

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
add_library(gpu_loader OBJECT Main.cpp)
2+
3+
include(FindLibcCommonUtils)
4+
target_link_libraries(gpu_loader PUBLIC llvm-libc-common-utilities)
5+
6+
target_include_directories(gpu_loader PUBLIC
7+
${CMAKE_CURRENT_SOURCE_DIR}
8+
${LIBC_SOURCE_DIR}/include
9+
${LIBC_SOURCE_DIR}
10+
${LLVM_MAIN_INCLUDE_DIR}
11+
${LLVM_BINARY_DIR}/include
12+
)
13+
if(NOT LLVM_ENABLE_RTTI)
14+
target_compile_options(gpu_loader PUBLIC -fno-rtti)
15+
endif()
16+
17+
find_package(hsa-runtime64 QUIET 1.2.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/rocm)
18+
if(hsa-runtime64_FOUND)
19+
add_subdirectory(amdgpu)
20+
endif()
21+
22+
# The CUDA loader requires LLVM to traverse the ELF image for symbols.
23+
find_package(CUDAToolkit 11.2 QUIET)
24+
if(CUDAToolkit_FOUND)
25+
add_subdirectory(nvptx)
26+
endif()
27+
28+
if(TARGET amdhsa-loader AND LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
29+
add_custom_target(libc.utils.gpu.loader)
30+
add_dependencies(libc.utils.gpu.loader amdhsa-loader)
31+
set_target_properties(
32+
libc.utils.gpu.loader
33+
PROPERTIES
34+
TARGET amdhsa-loader
35+
EXECUTABLE "$<TARGET_FILE:amdhsa-loader>"
36+
)
37+
elseif(TARGET nvptx-loader AND LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
38+
add_custom_target(libc.utils.gpu.loader)
39+
add_dependencies(libc.utils.gpu.loader nvptx-loader)
40+
set_target_properties(
41+
libc.utils.gpu.loader
42+
PROPERTIES
43+
TARGET nvptx-loader
44+
EXECUTABLE "$<TARGET_FILE:nvptx-loader>"
45+
)
46+
endif()
47+
48+
foreach(gpu_loader_tgt amdhsa-loader nvptx-loader)
49+
if(TARGET ${gpu_loader_tgt})
50+
install(TARGETS ${gpu_loader_tgt}
51+
DESTINATION ${CMAKE_INSTALL_BINDIR}
52+
COMPONENT libc)
53+
endif()
54+
endforeach()

llvm/tools/llvm-gpu-loader/llvm-gpu-loader.h renamed to libc/utils/gpu/loader/Loader.h

+3-10
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,9 @@ struct end_args_t {
5454
/// Generic interface to load the \p image and launch execution of the _start
5555
/// kernel on the target device. Copies \p argc and \p argv to the device.
5656
/// Returns the final value of the `main` function on the device.
57-
#ifdef AMDHSA_SUPPORT
58-
int load_amdhsa(int argc, const char **argv, const char **evnp, void *image,
59-
size_t size, const LaunchParameters &params,
60-
bool print_resource_usage);
61-
#endif
62-
#ifdef NVPTX_SUPPORT
63-
int load_nvptx(int argc, const char **argv, const char **evnp, void *image,
64-
size_t size, const LaunchParameters &params,
65-
bool print_resource_usage);
66-
#endif
57+
int load(int argc, const char **argv, const char **evnp, void *image,
58+
size_t size, const LaunchParameters &params,
59+
bool print_resource_usage);
6760

6861
/// Return \p V aligned "upwards" according to \p Align.
6962
template <typename V, typename A> inline V align_up(V val, A align) {

llvm/tools/llvm-gpu-loader/llvm-gpu-loader.cpp renamed to libc/utils/gpu/loader/Main.cpp

+7-39
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This utility is used to launch standard programs onto the GPU in conjunction
10-
// with the LLVM 'libc' project. It is designed to mimic a standard emulator
11-
// workflow, allowing for unit tests to be run on the GPU directly.
9+
// This file opens a device image passed on the command line and passes it to
10+
// one of the loader implementations for launch.
1211
//
1312
//===----------------------------------------------------------------------===//
1413

15-
#include "llvm-gpu-loader.h"
14+
#include "Loader.h"
1615

1716
#include "llvm/BinaryFormat/Magic.h"
18-
#include "llvm/Object/ELF.h"
19-
#include "llvm/Object/ELFObjectFile.h"
2017
#include "llvm/Support/CommandLine.h"
2118
#include "llvm/Support/Error.h"
2219
#include "llvm/Support/FileSystem.h"
2320
#include "llvm/Support/MemoryBuffer.h"
2421
#include "llvm/Support/Path.h"
2522
#include "llvm/Support/Signals.h"
2623
#include "llvm/Support/WithColor.h"
27-
#include "llvm/TargetParser/Triple.h"
2824

2925
#include <cerrno>
3026
#include <cstdio>
@@ -129,40 +125,12 @@ int main(int argc, const char **argv, const char **envp) {
129125
strerror(errno)));
130126
}
131127

128+
// Drop the loader from the program arguments.
132129
LaunchParameters params{threads_x, threads_y, threads_z,
133130
blocks_x, blocks_y, blocks_z};
134-
135-
Expected<llvm::object::ELF64LEObjectFile> elf_or_err =
136-
llvm::object::ELF64LEObjectFile::create(image);
137-
if (!elf_or_err)
138-
report_error(std::move(elf_or_err.takeError()));
139-
140-
int ret = 1;
141-
if (elf_or_err->getArch() == Triple::amdgcn) {
142-
#ifdef AMDHSA_SUPPORT
143-
ret = load_amdhsa(new_argv.size(), new_argv.data(), envp,
144-
const_cast<char *>(image.getBufferStart()),
145-
image.getBufferSize(), params, print_resource_usage);
146-
#else
147-
report_error(createStringError(
148-
"Unsupported architecture; %s",
149-
Triple::getArchTypeName(elf_or_err->getArch()).bytes_begin()));
150-
#endif
151-
} else if (elf_or_err->getArch() == Triple::nvptx64) {
152-
#ifdef NVPTX_SUPPORT
153-
ret = load_nvptx(new_argv.size(), new_argv.data(), envp,
154-
const_cast<char *>(image.getBufferStart()),
155-
image.getBufferSize(), params, print_resource_usage);
156-
#else
157-
report_error(createStringError(
158-
"Unsupported architecture; %s",
159-
Triple::getArchTypeName(elf_or_err->getArch()).bytes_begin()));
160-
#endif
161-
} else {
162-
report_error(createStringError(
163-
"Unsupported architecture; %s",
164-
Triple::getArchTypeName(elf_or_err->getArch()).bytes_begin()));
165-
}
131+
int ret = load(new_argv.size(), new_argv.data(), envp,
132+
const_cast<char *>(image.getBufferStart()),
133+
image.getBufferSize(), params, print_resource_usage);
166134

167135
if (no_parallelism) {
168136
if (flock(fd, LOCK_UN) == -1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set(LLVM_LINK_COMPONENTS
2+
BinaryFormat
3+
Object
4+
Option
5+
Support
6+
FrontendOffloading
7+
)
8+
9+
add_llvm_executable(amdhsa-loader amdhsa-loader.cpp)
10+
target_link_libraries(amdhsa-loader PRIVATE hsa-runtime64::hsa-runtime64 gpu_loader)

llvm/tools/llvm-gpu-loader/amdhsa.cpp renamed to libc/utils/gpu/loader/amdgpu/amdhsa-loader.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//
1414
//===----------------------------------------------------------------------===//
1515

16-
#include "llvm-gpu-loader.h"
16+
#include "Loader.h"
1717

1818
#include "hsa/hsa.h"
1919
#include "hsa/hsa_ext_amd.h"
@@ -330,9 +330,9 @@ static hsa_status_t hsa_memcpy(void *dst, hsa_agent_t dst_agent,
330330
return HSA_STATUS_SUCCESS;
331331
}
332332

333-
int load_amdhsa(int argc, const char **argv, const char **envp, void *image,
334-
size_t size, const LaunchParameters &params,
335-
bool print_resource_usage) {
333+
int load(int argc, const char **argv, const char **envp, void *image,
334+
size_t size, const LaunchParameters &params,
335+
bool print_resource_usage) {
336336
// Initialize the HSA runtime used to communicate with the device.
337337
if (hsa_status_t err = hsa_init())
338338
handle_error(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(LLVM_LINK_COMPONENTS
2+
BinaryFormat
3+
Object
4+
Option
5+
Support
6+
)
7+
8+
add_llvm_executable(nvptx-loader nvptx-loader.cpp)
9+
target_link_libraries(nvptx-loader PRIVATE gpu_loader CUDA::cuda_driver)

llvm/tools/llvm-gpu-loader/nvptx.cpp renamed to libc/utils/gpu/loader/nvptx/nvptx-loader.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//
1414
//===----------------------------------------------------------------------===//
1515

16-
#include "llvm-gpu-loader.h"
16+
#include "Loader.h"
1717

1818
#include "cuda.h"
1919

@@ -236,9 +236,9 @@ CUresult launch_kernel(CUmodule binary, CUstream stream, rpc::Server &server,
236236
return CUDA_SUCCESS;
237237
}
238238

239-
int load_nvptx(int argc, const char **argv, const char **envp, void *image,
240-
size_t size, const LaunchParameters &params,
241-
bool print_resource_usage) {
239+
int load(int argc, const char **argv, const char **envp, void *image,
240+
size_t size, const LaunchParameters &params,
241+
bool print_resource_usage) {
242242
if (CUresult err = cuInit(0))
243243
handle_error(err);
244244
// Obtain the first device found on the system.

llvm/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ if("${LIBC_TARGET_TRIPLE}" STREQUAL "amdgcn-amd-amdhsa" OR
210210
"${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda")
211211
set(LLVM_LIBC_GPU_BUILD ON)
212212
endif()
213+
if (NOT "libc" IN_LIST LLVM_ENABLE_PROJECTS AND LLVM_LIBC_GPU_BUILD)
214+
message(STATUS "Enabling libc project to build libc testing tools")
215+
list(APPEND LLVM_ENABLE_PROJECTS "libc")
216+
endif()
213217

214218
# LLVM_ENABLE_PROJECTS_USED is `ON` if the user has ever used the
215219
# `LLVM_ENABLE_PROJECTS` CMake cache variable. This exists for

llvm/runtimes/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,20 @@ if(build_runtimes)
534534
endif()
535535
if(LLVM_LIBC_GPU_BUILD)
536536
list(APPEND extra_cmake_args "-DLLVM_LIBC_GPU_BUILD=ON")
537+
if("libc" IN_LIST RUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES)
538+
if(TARGET amdhsa-loader)
539+
list(APPEND extra_cmake_args
540+
"-DRUNTIMES_amdgcn-amd-amdhsa_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:amdhsa-loader>")
541+
list(APPEND extra_deps amdhsa-loader)
542+
endif()
543+
endif()
544+
if("libc" IN_LIST RUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES)
545+
if(TARGET nvptx-loader)
546+
list(APPEND extra_cmake_args
547+
"-DRUNTIMES_nvptx64-nvidia-cuda_LIBC_GPU_LOADER_EXECUTABLE=$<TARGET_FILE:nvptx-loader>")
548+
list(APPEND extra_deps nvptx-loader)
549+
endif()
550+
endif()
537551
if(TARGET clang-offload-packager)
538552
list(APPEND extra_deps clang-offload-packager)
539553
endif()

llvm/tools/llvm-gpu-loader/CMakeLists.txt

-45
This file was deleted.

0 commit comments

Comments
 (0)