Skip to content
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

[SYCL] Turn read-only accessor iterator into const_iterator #7096

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion sycl/include/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,12 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
using reference = DataT &;
using const_reference = const DataT &;

using iterator = typename detail::accessor_iterator<DataT, Dimensions>;
// When accessor is read-only, we should not be able to modify it through the
// iterator and therefore, effectively returning const_iterator here
using iterator = typename detail::accessor_iterator<
AlexeySachkov marked this conversation as resolved.
Show resolved Hide resolved
typename std::conditional<AccessMode == access_mode::read, const DataT,
DataT>::type,
Dimensions>;
using const_iterator =
typename detail::accessor_iterator<const DataT, Dimensions>;
using difference_type =
Expand Down
65 changes: 65 additions & 0 deletions sycl/test/basic_tests/accessor/iterator-member-types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// RUN: %clangxx -fsycl -c %s
//
// Purpose of this test is to check that [accessor|host_accessor]::iterator and
// ::const_iterator are aliased to the correct type.
// FIXME: extend this test to also check ::reverse_iterator and
// ::const_reverse_iterator
#include <sycl/sycl.hpp>

#include <type_traits>

template <typename DataT, int Dimensions, sycl::access_mode AccessMode,
sycl::target AccessTarget = sycl::target::device>
void check_accessor() {
using IteratorDataT =
typename std::conditional<AccessMode == sycl::access_mode::read,
const DataT, DataT>::type;
static_assert(
std::is_same_v<sycl::detail::accessor_iterator<IteratorDataT, Dimensions>,
typename sycl::accessor<DataT, Dimensions, AccessMode,
AccessTarget>::iterator>);

// const_iterator should always be specialized as const DataT
static_assert(std::is_same_v<
sycl::detail::accessor_iterator<const DataT, Dimensions>,
typename sycl::accessor<DataT, Dimensions, AccessMode,
AccessTarget>::const_iterator>);
}

template <typename DataT, int Dimensions, sycl::access_mode AccessMode>
void check_host_accessor() {
using IteratorDataT =
typename std::conditional<AccessMode == sycl::access_mode::read,
const DataT, DataT>::type;
static_assert(
std::is_same_v<sycl::detail::accessor_iterator<IteratorDataT, Dimensions>,
typename sycl::host_accessor<DataT, Dimensions,
AccessMode>::iterator>);

// const_iterator should always be specialized as const DataT
static_assert(
std::is_same_v<sycl::detail::accessor_iterator<const DataT, Dimensions>,
typename sycl::host_accessor<DataT, Dimensions,
AccessMode>::const_iterator>);
}

struct user_defined_t {
char c;
float f;
double d;
sycl::vec<int, 3> v3;
};

int main() {

check_accessor<int, 1, sycl::access_mode::read>();
check_accessor<float, 2, sycl::access_mode::write>();
check_accessor<user_defined_t, 3, sycl::access_mode::read_write>();

check_host_accessor<user_defined_t, 1, sycl::access_mode::read>();
check_host_accessor<int, 2, sycl::access_mode::write>();
check_host_accessor<float, 3, sycl::access_mode::read_write>();

return 0;

}