Fix #118709: Crash in OIDN GPU detection for unsupported HIP device
[blender.git] / intern / cycles / device / hip / util.h
blobaf543f5eccbab7f347a182f8c1676e3334e97ab6
1 /* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 * SPDX-License-Identifier: Apache-2.0 */
5 #pragma once
7 #include <cstring>
8 #include <string>
10 #ifdef WITH_HIP
12 # ifdef WITH_HIP_DYNLOAD
13 # include "hipew.h"
14 # endif
16 CCL_NAMESPACE_BEGIN
18 class HIPDevice;
20 /* Utility to push/pop HIP context. */
21 class HIPContextScope {
22 public:
23 HIPContextScope(HIPDevice *device);
24 ~HIPContextScope();
26 private:
27 HIPDevice *device;
30 /* Utility for checking return values of HIP function calls. */
31 # define hip_device_assert(hip_device, stmt) \
32 { \
33 hipError_t result = stmt; \
34 if (result != hipSuccess) { \
35 const char *name = hipewErrorString(result); \
36 hip_device->set_error( \
37 string_printf("%s in %s (%s:%d)", name, #stmt, __FILE__, __LINE__)); \
38 } \
39 } \
40 (void)0
42 # define hip_assert(stmt) hip_device_assert(this, stmt)
44 # ifndef WITH_HIP_DYNLOAD
45 /* Transparently implement some functions, so majority of the file does not need
46 * to worry about difference between dynamically loaded and linked HIP at all. */
47 const char *hipewErrorString(hipError_t result);
48 const char *hipewCompilerPath();
49 int hipewCompilerVersion();
50 # endif /* WITH_HIP_DYNLOAD */
52 static std::string hipDeviceArch(const int hipDevId)
54 hipDeviceProp_t props;
55 hipGetDeviceProperties(&props, hipDevId);
56 const char *arch = strtok(props.gcnArchName, ":");
57 return (arch == nullptr) ? props.gcnArchName : arch;
60 static inline bool hipSupportsDevice(const int hipDevId)
62 int major, minor;
63 hipDeviceGetAttribute(&major, hipDeviceAttributeComputeCapabilityMajor, hipDevId);
64 hipDeviceGetAttribute(&minor, hipDeviceAttributeComputeCapabilityMinor, hipDevId);
66 return (major >= 9);
69 static inline bool hipSupportsDeviceOIDN(const int hipDevId)
71 /* Matches HIPDevice::getArch in HIP. */
72 const std::string arch = hipDeviceArch(hipDevId);
73 return (arch == "gfx1030" || arch == "gfx1100" || arch == "gfx1101" || arch == "gfx1102");
76 CCL_NAMESPACE_END
78 #endif /* WITH_HIP */