Skip to content

Commit 85d7a5e

Browse files
[SYCL] Fix numeric_limits<half> after #1089 (#1138)
Error was reproducible in two cases: - using something like `numeric_limits<half>::min()` in within another `constexpr` - not treating SYCL headers as system ones with `-Winvalid-constexpr` treated as error Signed-off-by: Alexey Sachkov <[email protected]>
1 parent 80b0306 commit 85d7a5e

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

sycl/include/CL/sycl/half_type.hpp

+30-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
#include <iostream>
1919
#include <limits>
2020

21+
#ifdef __SYCL_DEVICE_ONLY__
22+
// `constexpr` could work because the implicit conversion from `float` to
23+
// `_Float16` can be `constexpr`.
24+
#define __SYCL_CONSTEXPR_ON_DEVICE constexpr
25+
#else
26+
#define __SYCL_CONSTEXPR_ON_DEVICE
27+
#endif
28+
2129
__SYCL_INLINE_NAMESPACE(cl) {
2230
namespace sycl {
2331
namespace detail {
@@ -130,7 +138,7 @@ class half {
130138
half(const half &) = default;
131139
half(half &&) = default;
132140

133-
half(const float &rhs) : Data(rhs) {}
141+
__SYCL_CONSTEXPR_ON_DEVICE half(const float &rhs) : Data(rhs) {}
134142

135143
half &operator=(const half &rhs) = default;
136144

@@ -216,15 +224,6 @@ using half = cl::sycl::detail::half_impl::half;
216224
// Partial specialization of some functions in namespace `std`
217225
namespace std {
218226

219-
#ifdef __SYCL_DEVICE_ONLY__
220-
// `constexpr` could work because the implicit conversion from `float` to
221-
// `_Float16` can be `constexpr`.
222-
#define CONSTEXPR_QUALIFIER constexpr
223-
#else
224-
// The qualifier is `const` instead of `constexpr` that is original to be
225-
// because the constructor is not `constexpr` function.
226-
#define CONSTEXPR_QUALIFIER const
227-
#endif
228227

229228
// Partial specialization of `std::hash<cl::sycl::half>`
230229
template <> struct hash<half> {
@@ -307,35 +306,43 @@ template <> struct numeric_limits<half> {
307306

308307
static constexpr const float_round_style round_style = round_to_nearest;
309308

310-
static CONSTEXPR_QUALIFIER half min() noexcept { return SYCL_HLF_MIN; }
309+
static __SYCL_CONSTEXPR_ON_DEVICE const half min() noexcept {
310+
return SYCL_HLF_MIN;
311+
}
311312

312-
static CONSTEXPR_QUALIFIER half max() noexcept { return SYCL_HLF_MAX; }
313+
static __SYCL_CONSTEXPR_ON_DEVICE const half max() noexcept {
314+
return SYCL_HLF_MAX;
315+
}
313316

314-
static CONSTEXPR_QUALIFIER half lowest() noexcept { return -SYCL_HLF_MAX; }
317+
static __SYCL_CONSTEXPR_ON_DEVICE const half lowest() noexcept {
318+
return -SYCL_HLF_MAX;
319+
}
315320

316-
static CONSTEXPR_QUALIFIER half epsilon() noexcept {
321+
static __SYCL_CONSTEXPR_ON_DEVICE const half epsilon() noexcept {
317322
return SYCL_HLF_EPSILON;
318323
}
319324

320-
static CONSTEXPR_QUALIFIER half round_error() noexcept { return 0.5F; }
325+
static __SYCL_CONSTEXPR_ON_DEVICE const half round_error() noexcept {
326+
return 0.5F;
327+
}
321328

322-
static CONSTEXPR_QUALIFIER half infinity() noexcept {
329+
static __SYCL_CONSTEXPR_ON_DEVICE const half infinity() noexcept {
323330
return __builtin_huge_valf();
324331
}
325332

326-
static CONSTEXPR_QUALIFIER half quiet_NaN() noexcept {
333+
static __SYCL_CONSTEXPR_ON_DEVICE const half quiet_NaN() noexcept {
327334
return __builtin_nanf("");
328335
}
329336

330-
static CONSTEXPR_QUALIFIER half signaling_NaN() noexcept {
337+
static __SYCL_CONSTEXPR_ON_DEVICE const half signaling_NaN() noexcept {
331338
return __builtin_nansf("");
332339
}
333340

334-
static CONSTEXPR_QUALIFIER half denorm_min() noexcept { return 5.96046e-08F; }
341+
static __SYCL_CONSTEXPR_ON_DEVICE const half denorm_min() noexcept {
342+
return 5.96046e-08F;
343+
}
335344
};
336345

337-
#undef CONSTEXPR_QUALIFIER
338-
339346
} // namespace std
340347

341348
inline std::ostream &operator<<(std::ostream &O, half const &rhs) {
@@ -349,3 +356,5 @@ inline std::istream &operator>>(std::istream &I, half &rhs) {
349356
rhs = ValFloat;
350357
return I;
351358
}
359+
360+
#undef __SYCL_CONSTEXPR_ON_DEVICE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clangxx -fsycl-device-only -fsyntax-only -Xclang -verify %s
2+
// expected-no-diagnostics
3+
#include <CL/sycl.hpp>
4+
5+
int main() {
6+
constexpr cl::sycl::half L1 = std::numeric_limits<cl::sycl::half>::min();
7+
constexpr cl::sycl::half L2 = std::numeric_limits<cl::sycl::half>::max();
8+
constexpr cl::sycl::half L3 = std::numeric_limits<cl::sycl::half>::lowest();
9+
constexpr cl::sycl::half L4 = std::numeric_limits<cl::sycl::half>::epsilon();
10+
constexpr cl::sycl::half L5 =
11+
std::numeric_limits<cl::sycl::half>::round_error();
12+
constexpr cl::sycl::half L6 = std::numeric_limits<cl::sycl::half>::infinity();
13+
constexpr cl::sycl::half L7 =
14+
std::numeric_limits<cl::sycl::half>::quiet_NaN();
15+
constexpr cl::sycl::half L8 =
16+
std::numeric_limits<cl::sycl::half>::signaling_NaN();
17+
constexpr cl::sycl::half L9 =
18+
std::numeric_limits<cl::sycl::half>::denorm_min();
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)