From 5f796398d05621b61553128e3cb43a6859f2cf9c Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Fri, 20 Jan 2023 08:10:57 -0500 Subject: [PATCH 1/2] [SYCL] Make half default constructor constexpr Unfortunately, it seem to be impossible to have a trivial default constexpr constructor until we enable C++23 mode. Therefore, this patch made a few other adjustments to account for non-trivial default constructor of `half` class. --- sycl/include/sycl/bit_cast.hpp | 4 ++-- sycl/include/sycl/half_type.hpp | 9 +++++---- sycl/source/detail/builtins_relational.cpp | 1 + sycl/test/basic_tests/half-constexpr-constructor.cpp | 9 +++++++++ sycl/test/regression/half_union.cpp | 2 +- 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 sycl/test/basic_tests/half-constexpr-constructor.cpp diff --git a/sycl/include/sycl/bit_cast.hpp b/sycl/include/sycl/bit_cast.hpp index 342281b1a5785..f945ce6cfa9f6 100644 --- a/sycl/include/sycl/bit_cast.hpp +++ b/sycl/include/sycl/bit_cast.hpp @@ -41,8 +41,8 @@ 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::value, - "To must be trivially default constructible"); + static_assert(std::is_default_constructible::value, + "To must be default constructible"); To to; sycl::detail::memcpy(&to, &from, sizeof(To)); return to; diff --git a/sycl/include/sycl/half_type.hpp b/sycl/include/sycl/half_type.hpp index ace7be7352c6c..ec7a51104f7db 100644 --- a/sycl/include/sycl/half_type.hpp +++ b/sycl/include/sycl/half_type.hpp @@ -137,7 +137,8 @@ namespace host_half_impl { // The main host half class class __SYCL_EXPORT half { public: - half() = default; + constexpr half() {} + constexpr half(const half &) = default; constexpr half(half &&) = default; @@ -206,7 +207,7 @@ class __SYCL_EXPORT half { friend class sycl::ext::intel::esimd::detail::WrapperElementTypeProxy; private: - uint16_t Buf; + uint16_t Buf = {}; }; } // namespace host_half_impl @@ -272,7 +273,7 @@ class half { class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]] half { #endif public: - half() = default; + constexpr half() {} constexpr half(const half &) = default; constexpr half(half &&) = default; @@ -548,7 +549,7 @@ class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]] half { friend class sycl::ext::intel::esimd::detail::WrapperElementTypeProxy; private: - StorageT Data; + StorageT Data = {}; }; } // namespace half_impl diff --git a/sycl/source/detail/builtins_relational.cpp b/sycl/source/detail/builtins_relational.cpp index 8d64e1379733f..31bcd4b28271e 100644 --- a/sycl/source/detail/builtins_relational.cpp +++ b/sycl/source/detail/builtins_relational.cpp @@ -116,6 +116,7 @@ template <> union databitset { template <> union databitset { static_assert(sizeof(s::cl_short) == sizeof(s::cl_half), "size of cl_half is not equal to 16 bits(cl_short)."); + databitset(): i(0) {} s::cl_half f; s::cl_short i; }; diff --git a/sycl/test/basic_tests/half-constexpr-constructor.cpp b/sycl/test/basic_tests/half-constexpr-constructor.cpp new file mode 100644 index 0000000000000..6b0e640a7ba3b --- /dev/null +++ b/sycl/test/basic_tests/half-constexpr-constructor.cpp @@ -0,0 +1,9 @@ +// RUN: %clangxx -fsycl -fsyntax-only %s + +#include + +int main() { + constexpr sycl::half h; + + return 0; +} diff --git a/sycl/test/regression/half_union.cpp b/sycl/test/regression/half_union.cpp index 1b67f8cb76c39..8683bbb2af25a 100644 --- a/sycl/test/regression/half_union.cpp +++ b/sycl/test/regression/half_union.cpp @@ -5,7 +5,7 @@ typedef union _u16_to_half { unsigned short u; - sycl::half h; + sycl::half h = 0.0; } u16_to_sycl_half; int main() { From cf8afc297995303a5a85570a5d1b325d50477f8c Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Fri, 20 Jan 2023 08:25:47 -0500 Subject: [PATCH 2/2] Mark half constructor as default --- sycl/include/sycl/half_type.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/include/sycl/half_type.hpp b/sycl/include/sycl/half_type.hpp index ec7a51104f7db..16c47c4ca77f3 100644 --- a/sycl/include/sycl/half_type.hpp +++ b/sycl/include/sycl/half_type.hpp @@ -137,7 +137,7 @@ namespace host_half_impl { // The main host half class class __SYCL_EXPORT half { public: - constexpr half() {} + constexpr half() = default; constexpr half(const half &) = default; constexpr half(half &&) = default; @@ -273,7 +273,7 @@ class half { class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]] half { #endif public: - constexpr half() {} + constexpr half() = default; constexpr half(const half &) = default; constexpr half(half &&) = default;