[clang] 4ce454c - [Clang] Configure definitions for amdgpu/nvptx arch query tools
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 17 12:27:42 PST 2023
Author: Joseph Huber
Date: 2023-01-17T14:27:12-06:00
New Revision: 4ce454c654bd4e027fbdcd7b1dba9d164cd36463
URL: https://github.com/llvm/llvm-project/commit/4ce454c654bd4e027fbdcd7b1dba9d164cd36463
DIFF: https://github.com/llvm/llvm-project/commit/4ce454c654bd4e027fbdcd7b1dba9d164cd36463.diff
LOG: [Clang] Configure definitions for amdgpu/nvptx arch query tools
Summary:
These tools are built unconditionally now. However, there seemed to be
problems where the headers would be found during cross compilation, but
no libraries present. To combat this we should elect to make the CMake
indicate whether or not we should use the dynamic library method or link
it directly rather than using `__has_include`.
Added:
Modified:
clang/tools/amdgpu-arch/AMDGPUArch.cpp
clang/tools/amdgpu-arch/CMakeLists.txt
clang/tools/nvptx-arch/CMakeLists.txt
clang/tools/nvptx-arch/NVPTXArch.cpp
Removed:
################################################################################
diff --git a/clang/tools/amdgpu-arch/AMDGPUArch.cpp b/clang/tools/amdgpu-arch/AMDGPUArch.cpp
index caf8562c4e9dc..308bdeca243aa 100644
--- a/clang/tools/amdgpu-arch/AMDGPUArch.cpp
+++ b/clang/tools/amdgpu-arch/AMDGPUArch.cpp
@@ -17,33 +17,16 @@
#include <string>
#include <vector>
-#if defined(__has_include)
-#if __has_include("hsa/hsa.h")
-#define HSA_HEADER_FOUND 1
-#include "hsa/hsa.h"
-#elif __has_include("hsa.h")
-#define HSA_HEADER_FOUND 1
-#include "hsa.h"
-#else
-#define HSA_HEADER_FOUND 0
-#endif
-#else
-#define HSA_HEADER_FOUND 0
-#endif
-
-#if !HSA_HEADER_FOUND
-// Forward declaration of the status enumeration used by HSA.
+#if DYNAMIC_HSA
typedef enum {
HSA_STATUS_SUCCESS = 0x0,
} hsa_status_t;
-// Forward declaration of the device types used by HSA.
typedef enum {
HSA_DEVICE_TYPE_CPU = 0,
HSA_DEVICE_TYPE_GPU = 1,
} hsa_device_type_t;
-// Forward declaration of the agent information types we use.
typedef enum {
HSA_AGENT_INFO_NAME = 0,
HSA_AGENT_INFO_DEVICE = 17,
@@ -56,8 +39,7 @@ typedef struct hsa_agent_s {
hsa_status_t (*hsa_init)();
hsa_status_t (*hsa_shut_down)();
hsa_status_t (*hsa_agent_get_info)(hsa_agent_t, hsa_agent_info_t, void *);
-hsa_status_t (*hsa_iterate_agents)(hsa_status_t (*callback)(hsa_agent_t,
- void *),
+hsa_status_t (*hsa_iterate_agents)(hsa_status_t (*)(hsa_agent_t, void *),
void *);
constexpr const char *DynamicHSAPath = "libhsa-runtime64.so";
@@ -86,6 +68,16 @@ llvm::Error loadHSA() {
return llvm::Error::success();
}
#else
+
+#if defined(__has_include)
+#if __has_include("hsa/hsa.h")
+#include "hsa/hsa.h"
+#elif __has_include("hsa.h")
+#include "hsa.h"
+#endif
+#include "hsa/hsa.h"
+#endif
+
llvm::Error loadHSA() { return llvm::Error::success(); }
#endif
diff --git a/clang/tools/amdgpu-arch/CMakeLists.txt b/clang/tools/amdgpu-arch/CMakeLists.txt
index a5361e65758b1..d687d1691b7a6 100644
--- a/clang/tools/amdgpu-arch/CMakeLists.txt
+++ b/clang/tools/amdgpu-arch/CMakeLists.txt
@@ -15,4 +15,6 @@ find_package(hsa-runtime64 QUIET 1.2.0 HINTS ${CMAKE_INSTALL_PREFIX} PATHS /opt/
if (${hsa-runtime64_FOUND})
set_target_properties(amdgpu-arch PROPERTIES INSTALL_RPATH_USE_LINK_PATH ON)
clang_target_link_libraries(amdgpu-arch PRIVATE hsa-runtime64::hsa-runtime64)
+else()
+ target_compile_definitions(amdgpu-arch PRIVATE "DYNAMIC_HSA")
endif()
diff --git a/clang/tools/nvptx-arch/CMakeLists.txt b/clang/tools/nvptx-arch/CMakeLists.txt
index 1b6362c8f1951..9b31cdd772dcf 100644
--- a/clang/tools/nvptx-arch/CMakeLists.txt
+++ b/clang/tools/nvptx-arch/CMakeLists.txt
@@ -21,4 +21,6 @@ endif()
if (CUDA_FOUND AND cuda-library)
target_include_directories(nvptx-arch PRIVATE ${CUDA_INCLUDE_DIRS})
target_link_libraries(nvptx-arch PRIVATE ${cuda-library})
+else()
+ target_compile_definitions(nvptx-arch PRIVATE "DYNAMIC_CUDA")
endif()
diff --git a/clang/tools/nvptx-arch/NVPTXArch.cpp b/clang/tools/nvptx-arch/NVPTXArch.cpp
index acde975a34dc4..6509e42fee4e6 100644
--- a/clang/tools/nvptx-arch/NVPTXArch.cpp
+++ b/clang/tools/nvptx-arch/NVPTXArch.cpp
@@ -17,18 +17,7 @@
#include <cstdio>
#include <memory>
-#if defined(__has_include)
-#if __has_include("cuda.h")
-#include "cuda.h"
-#define CUDA_HEADER_FOUND 1
-#else
-#define CUDA_HEADER_FOUND 0
-#endif
-#else
-#define CUDA_HEADER_FOUND 0
-#endif
-
-#if !CUDA_HEADER_FOUND
+#if DYNAMIC_CUDA
typedef enum cudaError_enum {
CUDA_SUCCESS = 0,
CUDA_ERROR_NO_DEVICE = 100,
@@ -74,28 +63,31 @@ llvm::Error loadCUDA() {
return llvm::Error::success();
}
#else
+
+#include "cuda.h"
llvm::Error loadCUDA() { return llvm::Error::success(); }
+
#endif
static int handleError(CUresult Err) {
const char *ErrStr = nullptr;
CUresult Result = cuGetErrorString(Err, &ErrStr);
if (Result != CUDA_SUCCESS)
- return EXIT_FAILURE;
+ return 1;
fprintf(stderr, "CUDA error: %s\n", ErrStr);
- return EXIT_FAILURE;
+ return 1;
}
int main(int argc, char *argv[]) {
// Attempt to load the NVPTX driver runtime.
if (llvm::Error Err = loadCUDA()) {
logAllUnhandledErrors(std::move(Err), llvm::errs());
- return EXIT_FAILURE;
+ return 1;
}
if (CUresult Err = cuInit(0)) {
if (Err == CUDA_ERROR_NO_DEVICE)
- return EXIT_SUCCESS;
+ return 0;
else
return handleError(Err);
}
@@ -104,7 +96,7 @@ int main(int argc, char *argv[]) {
if (CUresult Err = cuDeviceGetCount(&Count))
return handleError(Err);
if (Count == 0)
- return EXIT_SUCCESS;
+ return 0;
for (int DeviceId = 0; DeviceId < Count; ++DeviceId) {
CUdevice Device;
if (CUresult Err = cuDeviceGet(&Device, DeviceId))
@@ -120,5 +112,5 @@ int main(int argc, char *argv[]) {
printf("sm_%d%d\n", Major, Minor);
}
- return EXIT_SUCCESS;
+ return 0;
}
More information about the cfe-commits
mailing list