Skip to content

Commit 0663dd6

Browse files
committed
GPU: provide the std type_traits subset used on Metal
MSL has no standard library, so the handful of traits the GPU code relies on are defined for __METAL__, as is already done for the other device compilers.
1 parent f2d5321 commit 0663dd6

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

GPU/Common/GPUCommonDef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "GPUCommonDefSettings.h"
3232

3333
#if !defined(__CLING__) && !defined(G__ROOT) // No GPU code for ROOT
34-
#if defined(__CUDACC__) || defined(__OPENCL__) || defined(__HIPCC__) || defined(__OPENCL_HOST__) || defined(__METAL_HOST__)
34+
#if defined(__CUDACC__) || defined(__OPENCL__) || defined(__HIPCC__) || defined(__OPENCL_HOST__) || defined(__METAL__) || defined(__METAL_HOST__)
3535
#define GPUCA_GPUCODE // Compiled by GPU compiler
3636
#endif
3737

GPU/Common/GPUCommonTypeTraits.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121
#ifndef GPUCA_GPUCODE_COMPILEKERNELS
2222
#include <type_traits>
2323
#endif
24+
#else // OpenCL C++ and Metal, neither of which provides <type_traits>
25+
// Spelled for MSL, which OpenCL C++ also accepts, so both backends share one implementation:
26+
// enum values because program scope variables must be constant (MSL 4.1 spec, sec. 4.2), and
27+
// is_pointer / is_member_pointer forward rather than inherit because MSL has no derived classes
28+
// (sec. 1.5.4). Bare T* / T& need no address space: a partial specialization matches them all.
29+
#ifdef __METAL__
30+
#define GPUCA_TT_PROGRAMSCOPE constant
2431
#else
32+
#define GPUCA_TT_PROGRAMSCOPE // not empty-by-default: in OpenCL 'constant' would mean __constant
33+
#endif
2534
namespace std
2635
{
2736
template <bool B, class T, class F>
@@ -37,14 +46,14 @@ using conditional_t = typename conditional<B, T, F>::type;
3746

3847
template <class T, class U>
3948
struct is_same {
40-
static constexpr bool value = false;
49+
enum { value = false };
4150
};
4251
template <class T>
4352
struct is_same<T, T> {
44-
static constexpr bool value = true;
53+
enum { value = true };
4554
};
4655
template <class T, class U>
47-
static constexpr bool is_same_v = is_same<T, U>::value;
56+
GPUCA_TT_PROGRAMSCOPE static constexpr bool is_same_v = is_same<T, U>::value;
4857

4958
template <bool B, class T = void>
5059
struct enable_if {
@@ -97,14 +106,15 @@ using remove_volatile_t = typename remove_volatile<T>::type;
97106

98107
template <class T>
99108
struct is_pointer_t {
100-
static constexpr bool value = false;
109+
enum { value = false };
101110
};
102111
template <class T>
103112
struct is_pointer_t<T*> {
104-
static constexpr bool value = true;
113+
enum { value = true };
105114
};
106115
template <class T>
107-
struct is_pointer : is_pointer_t<typename std::remove_cv<T>::type> {
116+
struct is_pointer {
117+
enum { value = is_pointer_t<typename std::remove_cv<T>::type>::value };
108118
};
109119

110120
template <class T>
@@ -124,19 +134,21 @@ using remove_reference_t = typename remove_reference<T>::type;
124134

125135
template <class T>
126136
struct is_member_pointer_helper {
127-
static constexpr bool value = false;
137+
enum { value = false };
128138
};
129139
template <class T, class U>
130140
struct is_member_pointer_helper<T U::*> {
131-
static constexpr bool value = true;
141+
enum { value = true };
132142
};
133143
template <class T>
134-
struct is_member_pointer : is_member_pointer_helper<typename std::remove_cv<T>::type> {
144+
struct is_member_pointer {
145+
enum { value = is_member_pointer_helper<typename std::remove_cv<T>::type>::value };
135146
};
136147
template <class T>
137-
static constexpr bool is_member_pointer_v = is_member_pointer<T>::value;
148+
GPUCA_TT_PROGRAMSCOPE static constexpr bool is_member_pointer_v = is_member_pointer<T>::value;
138149

139150
} // namespace std
151+
#undef GPUCA_TT_PROGRAMSCOPE
140152
#endif
141153

142154
#endif

0 commit comments

Comments
 (0)