[Clang] Configure definitions for amdgpu/nvptx arch query tools
authorJoseph Huber <jhuber6@vols.utk.edu>
Tue, 17 Jan 2023 20:25:29 +0000 (14:25 -0600)
committerJoseph Huber <jhuber6@vols.utk.edu>
Tue, 17 Jan 2023 20:27:12 +0000 (14:27 -0600)
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`.

clang/tools/amdgpu-arch/AMDGPUArch.cpp
clang/tools/amdgpu-arch/CMakeLists.txt
clang/tools/nvptx-arch/CMakeLists.txt
clang/tools/nvptx-arch/NVPTXArch.cpp

index caf8562..308bdec 100644 (file)
 #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
 
index a5361e6..d687d16 100644 (file)
@@ -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()
index 1b6362c..9b31cdd 100644 (file)
@@ -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()
index acde975..6509e42 100644 (file)
 #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;
 }