Skip to content

Commit d32a444

Browse files
authored
[SYCL] Add half constexpr default constructor (#4518)
Add default constexpr constructor for half and host half classes, and also for half_vec struct. Due to bitselect built-in uses union with half specialization, but object of a class without trivial constructor cannot be a member of union, the builtins_helper struct was added. This struct allow to use host half class instead of main half class in the builtin. Signed-off-by: mdimakov [email protected]
1 parent dd7f82c commit d32a444

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

sycl/include/CL/sycl/half_type.hpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ __SYCL_INLINE_NAMESPACE(cl) {
3737
namespace sycl {
3838
namespace detail {
3939

40+
// bitselect builtin uses union with half specialization, but half has no
41+
// trivial constructor. This struct allow to use host half class instead of main
42+
// half class in the bitselect builtin.
43+
template <typename T> struct builtins_helper;
44+
4045
inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
4146
const uint32_t Bits = sycl::bit_cast<uint32_t>(Val);
4247

@@ -189,7 +194,7 @@ class __SYCL_EXPORT half {
189194
// The main host half class
190195
class __SYCL_EXPORT half_v2 {
191196
public:
192-
half_v2() = default;
197+
__SYCL_CONSTEXPR_HALF half_v2() : Buf(float2Half(0.0f)) {}
193198
constexpr half_v2(const half_v2 &) = default;
194199
constexpr half_v2(half_v2 &&) = default;
195200

@@ -255,6 +260,8 @@ class __SYCL_EXPORT half_v2 {
255260
// Initialize underlying data
256261
constexpr explicit half_v2(uint16_t x) : Buf(x) {}
257262

263+
template <typename T> friend struct cl::sycl::detail::builtins_helper;
264+
258265
private:
259266
uint16_t Buf;
260267
};
@@ -299,7 +306,12 @@ using BIsRepresentationT = half;
299306
// as a kernel argument which is expected to be floating point number.
300307
template <int NumElements> struct half_vec {
301308
alignas(detail::vector_alignment<StorageT, NumElements>::value)
302-
std::array<StorageT, NumElements> s;
309+
StorageT s[NumElements];
310+
311+
__SYCL_CONSTEXPR_HALF half_vec() {
312+
for (int i = 0; i < NumElements; i++)
313+
s[i] = StorageT(0.0f);
314+
}
303315
};
304316

305317
using Vec2StorageT = half_vec<2>;
@@ -311,7 +323,7 @@ template <int NumElements> struct half_vec {
311323

312324
class half {
313325
public:
314-
half() = default;
326+
__SYCL_CONSTEXPR_HALF half() : Data(0.0f){};
315327
constexpr half(const half &) = default;
316328
constexpr half(half &&) = default;
317329

@@ -383,6 +395,8 @@ class half {
383395
}
384396

385397
template <typename Key> friend struct std::hash;
398+
template <typename T> friend struct cl::sycl::detail::builtins_helper;
399+
386400
private:
387401
StorageT Data;
388402
};

sycl/source/detail/builtins_relational.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ namespace s = cl::sycl;
1818
namespace d = s::detail;
1919

2020
__SYCL_INLINE_NAMESPACE(cl) {
21+
namespace sycl {
22+
namespace detail {
23+
24+
// The declaration of this struct is in the CL/sycl/half_type.hpp
25+
template <typename T> struct builtins_helper {
26+
using RetType = T;
27+
static constexpr RetType get(T value) { return value; }
28+
};
29+
30+
template <> struct builtins_helper<s::cl_half> {
31+
using RetType = uint16_t;
32+
static constexpr RetType get(s::cl_half value) { return value.Data.Buf; }
33+
};
34+
} // namespace detail
35+
} // namespace sycl
2136
namespace __host_std {
2237
namespace {
2338

@@ -103,19 +118,20 @@ template <> union databitset<s::cl_double> {
103118
template <> union databitset<s::cl_half> {
104119
static_assert(sizeof(s::cl_short) == sizeof(s::cl_half),
105120
"size of cl_half is not equal to 16 bits(cl_short).");
106-
s::cl_half f;
121+
uint16_t f;
107122
s::cl_short i;
108123
};
109124

110125
template <typename T>
111126
typename sycl::detail::enable_if_t<d::is_sgenfloat<T>::value,
112127
T> inline __bitselect(T a, T b, T c) {
128+
d::builtins_helper<T> helper;
113129
databitset<T> ba;
114-
ba.f = a;
130+
ba.f = helper.get(a);
115131
databitset<T> bb;
116-
bb.f = b;
132+
bb.f = helper.get(b);
117133
databitset<T> bc;
118-
bc.f = c;
134+
bc.f = helper.get(c);
119135
databitset<T> br;
120136
br.f = 0;
121137
br.i = ((ba.i & ~bc.i) | (bb.i & bc.i));

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBM@Z
341341
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBV01234@@Z
342342
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@G@Z
343+
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@XZ
343344
??0handler@sycl@cl@@AEAA@V?$shared_ptr@Vqueue_impl@detail@sycl@cl@@@std@@_N@Z
344345
??0host_selector@sycl@cl@@QEAA@$$QEAV012@@Z
345346
??0host_selector@sycl@cl@@QEAA@AEBV012@@Z

sycl/test/basic_tests/types.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ template <> inline void checkSizeForFloatingPoint<s::half, sizeof(int16_t)>() {
9090
}
9191

9292
int main() {
93+
// Test for half constexpr default constructors
94+
constexpr sycl::specialization_id<sycl::vec<sycl::half, 2>> id(1.0);
95+
constexpr sycl::marray<sycl::half, 2> MH(3);
9396
// Check the size and alignment of the SYCL vectors.
9497
checkVectors();
9598

0 commit comments

Comments
 (0)