Skip to content

[SYCL] Return trivial default constructor to half type #4631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 4, 2021
Merged
19 changes: 1 addition & 18 deletions sycl/include/CL/sycl/bit_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#pragma once

#include <cstdint>
#include <type_traits>

#if __cpp_lib_bit_cast
Expand All @@ -21,21 +20,6 @@ namespace sycl {
// forward decl
namespace detail {
inline void memcpy(void *Dst, const void *Src, std::size_t Size);

namespace half_impl {
class half;
}
using half = cl::sycl::detail::half_impl::half;

template <typename T>
#ifdef __SYCL_DEVICE_ONLY__
using lowering_half_type =
typename std::conditional<std::is_same<T, half>::value, _Float16, T>::type;
#else
using lowering_half_type =
typename std::conditional<std::is_same<T, half>::value, std::uint16_t,
T>::type;
#endif
}

template <typename To, typename From>
Expand All @@ -57,8 +41,7 @@ constexpr
#if __has_builtin(__builtin_bit_cast)
return __builtin_bit_cast(To, from);
#else // __has_builtin(__builtin_bit_cast)
static_assert(std::is_trivially_default_constructible<
typename sycl::detail::lowering_half_type<To>>::value,
static_assert(std::is_trivially_default_constructible<To>::value,
"To must be trivially default constructible");
To to;
sycl::detail::memcpy(&to, &from, sizeof(To));
Expand Down
18 changes: 6 additions & 12 deletions sycl/include/CL/sycl/half_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ __SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

// bitselect builtin uses union with half specialization, but half has no
// trivial constructor. This struct allow to use host half class instead of main
// half class in the bitselect builtin.
template <typename T> struct builtins_helper;

inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
const uint32_t Bits = sycl::bit_cast<uint32_t>(Val);

Expand Down Expand Up @@ -194,7 +189,7 @@ class __SYCL_EXPORT half {
// The main host half class
class __SYCL_EXPORT half_v2 {
public:
__SYCL_CONSTEXPR_HALF half_v2() : Buf(float2Half(0.0f)) {}
half_v2() = default;
constexpr half_v2(const half_v2 &) = default;
constexpr half_v2(half_v2 &&) = default;

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

template <typename T> friend struct cl::sycl::detail::builtins_helper;

private:
uint16_t Buf;
};
Expand Down Expand Up @@ -308,9 +301,11 @@ template <int NumElements> struct half_vec {
alignas(detail::vector_alignment<StorageT, NumElements>::value)
StorageT s[NumElements];

__SYCL_CONSTEXPR_HALF half_vec() {
for (int i = 0; i < NumElements; i++)
__SYCL_CONSTEXPR_HALF half_vec() : s{0.0f} { initialize_data(); }
constexpr void initialize_data() {
for (size_t i = 0; i < NumElements; ++i) {
s[i] = StorageT(0.0f);
}
}
};

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

class half {
public:
__SYCL_CONSTEXPR_HALF half() : Data(0.0f){};
half() = default;
constexpr half(const half &) = default;
constexpr half(half &&) = default;

Expand Down Expand Up @@ -395,7 +390,6 @@ class half {
}

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

private:
StorageT Data;
Expand Down
12 changes: 8 additions & 4 deletions sycl/include/CL/sycl/marray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ template <typename Type, std::size_t NumElements> class marray {
using EnableIfSuitableTypes = typename std::enable_if<
conjunction<TypeChecker<ArgTN, DataT>...>::value>::type;

constexpr void initialize_data(const Type &Arg) {
for (size_t i = 0; i < NumElements; ++i) {
MData[i] = Arg;
}
}

public:
constexpr marray() : MData{} {}

explicit constexpr marray(const Type &Arg) {
for (std::size_t I = 0; I < NumElements; ++I) {
MData[I] = Arg;
}
explicit constexpr marray(const Type &Arg) : MData{Arg} {
initialize_data(Arg);
}

template <
Expand Down
24 changes: 4 additions & 20 deletions sycl/source/detail/builtins_relational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ namespace s = cl::sycl;
namespace d = s::detail;

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

// The declaration of this struct is in the CL/sycl/half_type.hpp
template <typename T> struct builtins_helper {
using RetType = T;
static constexpr RetType get(T value) { return value; }
};

template <> struct builtins_helper<s::cl_half> {
using RetType = uint16_t;
static constexpr RetType get(s::cl_half value) { return value.Data.Buf; }
};
} // namespace detail
} // namespace sycl
namespace __host_std {
namespace {

Expand Down Expand Up @@ -118,20 +103,19 @@ template <> union databitset<s::cl_double> {
template <> union databitset<s::cl_half> {
static_assert(sizeof(s::cl_short) == sizeof(s::cl_half),
"size of cl_half is not equal to 16 bits(cl_short).");
uint16_t f;
s::cl_half f;
s::cl_short i;
};

template <typename T>
typename sycl::detail::enable_if_t<d::is_sgenfloat<T>::value,
T> inline __bitselect(T a, T b, T c) {
d::builtins_helper<T> helper;
databitset<T> ba;
ba.f = helper.get(a);
ba.f = a;
databitset<T> bb;
bb.f = helper.get(b);
bb.f = b;
databitset<T> bc;
bc.f = helper.get(c);
bc.f = c;
databitset<T> br;
br.f = 0;
br.i = ((ba.i & ~bc.i) | (bb.i & bc.i));
Expand Down
1 change: 0 additions & 1 deletion sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBM@Z
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@AEBV01234@@Z
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@G@Z
??0half_v2@host_half_impl@detail@sycl@cl@@QEAA@XZ
??0handler@sycl@cl@@AEAA@V?$shared_ptr@Vqueue_impl@detail@sycl@cl@@@std@@_N@Z
??0host_selector@sycl@cl@@QEAA@$$QEAV012@@Z
??0host_selector@sycl@cl@@QEAA@AEBV012@@Z
Expand Down
2 changes: 1 addition & 1 deletion sycl/test/basic_tests/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ template <> inline void checkSizeForFloatingPoint<s::half, sizeof(int16_t)>() {
}

int main() {
// Test for half constexpr default constructors
// Test for creating constexpr expressions
constexpr sycl::specialization_id<sycl::vec<sycl::half, 2>> id(1.0);
constexpr sycl::marray<sycl::half, 2> MH(3);
// Check the size and alignment of the SYCL vectors.
Expand Down
14 changes: 14 additions & 0 deletions sycl/test/regression/half_union.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %RUN_ON_HOST %t.out

#include <CL/sycl.hpp>

typedef union _u16_to_half {
unsigned short u;
cl::sycl::half h;
} u16_to_sycl_half;

int main() {
u16_to_sycl_half unh;
return 0;
}