diff --git a/sycl/include/sycl/item.hpp b/sycl/include/sycl/item.hpp index bf9482cfdb110..4246940913872 100644 --- a/sycl/include/sycl/item.hpp +++ b/sycl/include/sycl/item.hpp @@ -50,51 +50,54 @@ template class item { public: item() = delete; - id get_id() const { return MImpl.MIndex; } + id get_id() const noexcept { return MImpl.MIndex; } - size_t __SYCL_ALWAYS_INLINE get_id(int Dimension) const { + size_t __SYCL_ALWAYS_INLINE get_id(int Dimension) const noexcept { size_t Id = MImpl.MIndex[Dimension]; __SYCL_ASSUME_ID_RANGE(Id); return Id; } - size_t __SYCL_ALWAYS_INLINE operator[](int Dimension) const { + size_t __SYCL_ALWAYS_INLINE operator[](int Dimension) const noexcept { size_t Id = MImpl.MIndex[Dimension]; __SYCL_ASSUME_ID_RANGE(Id); return Id; } - range get_range() const { return MImpl.MExtent; } + range get_range() const noexcept { return MImpl.MExtent; } - size_t __SYCL_ALWAYS_INLINE get_range(int Dimension) const { + size_t __SYCL_ALWAYS_INLINE get_range(int Dimension) const noexcept { size_t Id = MImpl.MExtent[Dimension]; __SYCL_ASSUME_ID_RANGE(Id); return Id; } #ifndef __SYCL_DISABLE_ITEM_TO_INT_CONV__ - operator EnableIfT() const { return get_id(0); } + operator EnableIfT() const noexcept { + return get_id(0); + } #endif // __SYCL_DISABLE_ITEM_TO_INT_CONV__ template __SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020") - std::enable_if_t> get_offset() const { + std::enable_if_t> get_offset() const noexcept { return MImpl.MOffset; } template __SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020") std::enable_if_t __SYCL_ALWAYS_INLINE - get_offset(int Dimension) const { + get_offset(int Dimension) const noexcept { size_t Id = MImpl.MOffset[Dimension]; __SYCL_ASSUME_ID_RANGE(Id); return Id; } template - operator std::enable_if_t>() const { + operator std::enable_if_t>() + const noexcept { return item{MImpl.MExtent, MImpl.MIndex, /*Offset*/ {}}; } - size_t __SYCL_ALWAYS_INLINE get_linear_id() const { + size_t __SYCL_ALWAYS_INLINE get_linear_id() const noexcept { size_t Id = MImpl.get_linear_id(); __SYCL_ASSUME_ID_RANGE(Id); return Id; @@ -108,9 +111,13 @@ template class item { item &operator=(item &&rhs) = default; - bool operator==(const item &rhs) const { return rhs.MImpl == MImpl; } + friend bool operator==(const item &lhs, const item &rhs) { + return lhs.MImpl == rhs.MImpl; + } - bool operator!=(const item &rhs) const { return rhs.MImpl != MImpl; } + friend bool operator!=(const item &lhs, const item &rhs) { + return !(lhs == rhs); + } protected: template diff --git a/sycl/test/basic_tests/item_api.cpp b/sycl/test/basic_tests/item_api.cpp new file mode 100644 index 0000000000000..75213776248bf --- /dev/null +++ b/sycl/test/basic_tests/item_api.cpp @@ -0,0 +1,64 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -Wno-deprecated-declarations -fsyntax-only %s +//==----------- item_api.cpp - SYCL item API test --------------------------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include +#include +#include + +template +struct HasMemberEquality : std::false_type {}; + +template +struct HasMemberEquality< + T, std::void_t().operator==( + std::declval()))>> : std::true_type {}; + +template +struct HasMemberInequality : std::false_type {}; + +template +struct HasMemberInequality< + T, std::void_t().operator!=( + std::declval()))>> : std::true_type {}; + +using ItemWithOffset = sycl::item<2, true>; +using ItemWithoutOffset = sycl::item<2, false>; +using OneDimItem = sycl::item<1, false>; + +static_assert(noexcept(std::declval().get_id())); +static_assert(noexcept(std::declval().get_id(0))); +static_assert(noexcept(std::declval()[0])); +static_assert(noexcept(std::declval().get_range())); +static_assert(noexcept(std::declval().get_range(0))); +static_assert(noexcept(std::declval().get_offset())); +static_assert(noexcept(std::declval().get_offset(0))); +static_assert(noexcept(std::declval().get_linear_id())); +static_assert( + noexcept(static_cast(std::declval()))); +static_assert(noexcept(static_cast>( + std::declval()))); +static_assert(std::is_same_v() == + std::declval()), + bool>); +static_assert(std::is_same_v() != + std::declval()), + bool>); + +static_assert(!HasMemberEquality::value); +static_assert(!HasMemberInequality::value); +static_assert( + std::is_same_v(), + std::declval())), + bool>); +static_assert( + std::is_same_v(), + std::declval())), + bool>);