Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions sycl/include/sycl/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,51 +50,54 @@ template <int Dimensions = 1, bool with_offset = true> class item {
public:
item() = delete;

id<Dimensions> get_id() const { return MImpl.MIndex; }
id<Dimensions> 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<Dimensions> get_range() const { return MImpl.MExtent; }
range<Dimensions> 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<Dimensions == 1, std::size_t>() const { return get_id(0); }
operator EnableIfT<Dimensions == 1, std::size_t>() const noexcept {
return get_id(0);
}
#endif // __SYCL_DISABLE_ITEM_TO_INT_CONV__
template <bool has_offset = with_offset>
__SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020")
std::enable_if_t<has_offset, id<Dimensions>> get_offset() const {
std::enable_if_t<has_offset, id<Dimensions>> get_offset() const noexcept {
return MImpl.MOffset;
}

template <bool has_offset = with_offset>
__SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020")
std::enable_if_t<has_offset, size_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 <bool has_offset = with_offset>
operator std::enable_if_t<!has_offset, item<Dimensions, true>>() const {
operator std::enable_if_t<!has_offset, item<Dimensions, true>>()
const noexcept {
return item<Dimensions, true>{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;
Expand All @@ -108,9 +111,13 @@ template <int Dimensions = 1, bool with_offset = true> 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 <bool has_offset = with_offset>
Expand Down
64 changes: 64 additions & 0 deletions sycl/test/basic_tests/item_api.cpp
Original file line number Diff line number Diff line change
@@ -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 <sycl/item.hpp>

#include <cstddef>
#include <type_traits>
#include <utility>

template <typename T, typename = void>
struct HasMemberEquality : std::false_type {};

template <typename T>
struct HasMemberEquality<
T, std::void_t<decltype(std::declval<const T &>().operator==(
std::declval<const T &>()))>> : std::true_type {};

template <typename T, typename = void>
struct HasMemberInequality : std::false_type {};

template <typename T>
struct HasMemberInequality<
T, std::void_t<decltype(std::declval<const T &>().operator!=(
std::declval<const T &>()))>> : 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<const ItemWithOffset &>().get_id()));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_id(0)));
static_assert(noexcept(std::declval<const ItemWithOffset &>()[0]));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_range()));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_range(0)));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_offset()));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_offset(0)));
static_assert(noexcept(std::declval<const ItemWithOffset &>().get_linear_id()));
static_assert(
noexcept(static_cast<std::size_t>(std::declval<const OneDimItem &>())));
static_assert(noexcept(static_cast<sycl::item<2, true>>(
std::declval<const ItemWithoutOffset &>())));
static_assert(std::is_same_v<decltype(std::declval<const ItemWithOffset &>() ==
std::declval<const ItemWithOffset &>()),
bool>);
static_assert(std::is_same_v<decltype(std::declval<const ItemWithOffset &>() !=
std::declval<const ItemWithOffset &>()),
bool>);

static_assert(!HasMemberEquality<ItemWithOffset>::value);
static_assert(!HasMemberInequality<ItemWithOffset>::value);
static_assert(
std::is_same_v<decltype(operator==(std::declval<const ItemWithOffset &>(),
std::declval<const ItemWithOffset &>())),
bool>);
static_assert(
std::is_same_v<decltype(operator!=(std::declval<const ItemWithOffset &>(),
std::declval<const ItemWithOffset &>())),
bool>);
Loading