Skip to content

Commit 909a824

Browse files
authored
[SYCL] Return trivial default constructor to half type (#4631)
Return trivial default constructor to half type. Without default constructor it is impossible to create union with half type. Changed implementation of marray and half_vec constructors for optimizing them to use half with trivial default constructor. Update files accordingly to returned half trivial default constructor. This patch corrects #4518 Signed-off-by: mdimakov maxim.dimakov@intel.com
1 parent 7e11e48 commit 909a824

File tree

7 files changed

+34
-56
lines changed

7 files changed

+34
-56
lines changed

sycl/include/CL/sycl/bit_cast.hpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#pragma once
1010

11-
#include <cstdint>
1211
#include <type_traits>
1312

1413
#if __cpp_lib_bit_cast
@@ -21,21 +20,6 @@ namespace sycl {
2120
// forward decl
2221
namespace detail {
2322
inline void memcpy(void *Dst, const void *Src, std::size_t Size);
24-
25-
namespace half_impl {
26-
class half;
27-
}
28-
using half = cl::sycl::detail::half_impl::half;
29-
30-
template <typename T>
31-
#ifdef __SYCL_DEVICE_ONLY__
32-
using lowering_half_type =
33-
typename std::conditional<std::is_same<T, half>::value, _Float16, T>::type;
34-
#else
35-
using lowering_half_type =
36-
typename std::conditional<std::is_same<T, half>::value, std::uint16_t,
37-
T>::type;
38-
#endif
3923
}
4024

4125
template <typename To, typename From>
@@ -57,8 +41,7 @@ constexpr
5741
#if __has_builtin(__builtin_bit_cast)
5842
return __builtin_bit_cast(To, from);
5943
#else // __has_builtin(__builtin_bit_cast)
60-
static_assert(std::is_trivially_default_constructible<
61-
typename sycl::detail::lowering_half_type<To>>::value,
44+
static_assert(std::is_trivially_default_constructible<To>::value,
6245
"To must be trivially default constructible");
6346
To to;
6447
sycl::detail::memcpy(&to, &from, sizeof(To));

sycl/include/CL/sycl/half_type.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ __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-
4540
inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
4641
const uint32_t Bits = sycl::bit_cast<uint32_t>(Val);
4742

@@ -194,7 +189,7 @@ class __SYCL_EXPORT half {
194189
// The main host half class
195190
class __SYCL_EXPORT half_v2 {
196191
public:
197-
__SYCL_CONSTEXPR_HALF half_v2() : Buf(float2Half(0.0f)) {}
192+
half_v2() = default;
198193
constexpr half_v2(const half_v2 &) = default;
199194
constexpr half_v2(half_v2 &&) = default;
200195

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

263-
template <typename T> friend struct cl::sycl::detail::builtins_helper;
264-
265258
private:
266259
uint16_t Buf;
267260
};
@@ -308,9 +301,11 @@ template <int NumElements> struct half_vec {
308301
alignas(detail::vector_alignment<StorageT, NumElements>::value)
309302
StorageT s[NumElements];
310303

311-
__SYCL_CONSTEXPR_HALF half_vec() {
312-
for (int i = 0; i < NumElements; i++)
304+
__SYCL_CONSTEXPR_HALF half_vec() : s{0.0f} { initialize_data(); }
305+
constexpr void initialize_data() {
306+
for (size_t i = 0; i < NumElements; ++i) {
313307
s[i] = StorageT(0.0f);
308+
}
314309
}
315310
};
316311

@@ -323,7 +318,7 @@ template <int NumElements> struct half_vec {
323318

324319
class half {
325320
public:
326-
__SYCL_CONSTEXPR_HALF half() : Data(0.0f){};
321+
half() = default;
327322
constexpr half(const half &) = default;
328323
constexpr half(half &&) = default;
329324

@@ -395,7 +390,6 @@ class half {
395390
}
396391

397392
template <typename Key> friend struct std::hash;
398-
template <typename T> friend struct cl::sycl::detail::builtins_helper;
399393

400394
private:
401395
StorageT Data;

sycl/include/CL/sycl/marray.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ template <typename Type, std::size_t NumElements> class marray {
4848
using EnableIfSuitableTypes = typename std::enable_if<
4949
conjunction<TypeChecker<ArgTN, DataT>...>::value>::type;
5050

51+
constexpr void initialize_data(const Type &Arg) {
52+
for (size_t i = 0; i < NumElements; ++i) {
53+
MData[i] = Arg;
54+
}
55+
}
56+
5157
public:
5258
constexpr marray() : MData{} {}
5359

54-
explicit constexpr marray(const Type &Arg) {
55-
for (std::size_t I = 0; I < NumElements; ++I) {
56-
MData[I] = Arg;
57-
}
60+
explicit constexpr marray(const Type &Arg) : MData{Arg} {
61+
initialize_data(Arg);
5862
}
5963

6064
template <

sycl/source/detail/builtins_relational.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@ 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
3621
namespace __host_std {
3722
namespace {
3823

@@ -118,20 +103,19 @@ template <> union databitset<s::cl_double> {
118103
template <> union databitset<s::cl_half> {
119104
static_assert(sizeof(s::cl_short) == sizeof(s::cl_half),
120105
"size of cl_half is not equal to 16 bits(cl_short).");
121-
uint16_t f;
106+
s::cl_half f;
122107
s::cl_short i;
123108
};
124109

125110
template <typename T>
126111
typename sycl::detail::enable_if_t<d::is_sgenfloat<T>::value,
127112
T> inline __bitselect(T a, T b, T c) {
128-
d::builtins_helper<T> helper;
129113
databitset<T> ba;
130-
ba.f = helper.get(a);
114+
ba.f = a;
131115
databitset<T> bb;
132-
bb.f = helper.get(b);
116+
bb.f = b;
133117
databitset<T> bc;
134-
bc.f = helper.get(c);
118+
bc.f = c;
135119
databitset<T> br;
136120
br.f = 0;
137121
br.i = ((ba.i & ~bc.i) | (bb.i & bc.i));

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@
341341
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBM@Z
342342
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBV01234@@Z
343343
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@G@Z
344-
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@XZ
345344
??0handler@sycl@cl@@AEAA@V?$shared_ptr@Vqueue_impl@detail@sycl@cl@@@std@@_N@Z
346345
??0host_selector@sycl@cl@@QEAA@$$QEAV012@@Z
347346
??0host_selector@sycl@cl@@QEAA@AEBV012@@Z

sycl/test/basic_tests/types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ template <> inline void checkSizeForFloatingPoint<s::half, sizeof(int16_t)>() {
9090
}
9191

9292
int main() {
93-
// Test for half constexpr default constructors
93+
// Test for creating constexpr expressions
9494
constexpr sycl::specialization_id<sycl::vec<sycl::half, 2>> id(1.0);
9595
constexpr sycl::marray<sycl::half, 2> MH(3);
9696
// Check the size and alignment of the SYCL vectors.

sycl/test/regression/half_union.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %RUN_ON_HOST %t.out
3+
4+
#include <CL/sycl.hpp>
5+
6+
typedef union _u16_to_half {
7+
unsigned short u;
8+
cl::sycl::half h;
9+
} u16_to_sycl_half;
10+
11+
int main() {
12+
u16_to_sycl_half unh;
13+
return 0;
14+
}

0 commit comments

Comments
 (0)