Skip to content

Commit

Permalink
[SYCL] Adjust accessor::value_type in accordance with the spec (#7096)
Browse files Browse the repository at this point in the history
`value_type` should be defined as `const DataT` for read-only accessors.
This also makes `accessor::iterator` of a read-only accessor non-writeable
as it should be in accordance with the spec.
  • Loading branch information
AlexeySachkov authored Oct 20, 2022
1 parent 7a86aac commit 227614c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sycl/include/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,13 +1205,17 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
friend class sycl::ext::intel::esimd::detail::AccessorPrivateProxy;

public:
using value_type = DataT;
// 4.7.6.9.1. Interface for buffer command accessors
// value_type is defined as const DataT for read_only accessors, DataT
// otherwise
using value_type = typename std::conditional<AccessMode == access_mode::read,
const DataT, DataT>::type;
using reference = DataT &;
using const_reference = const DataT &;

using iterator = typename detail::accessor_iterator<DataT, Dimensions>;
using iterator = typename detail::accessor_iterator<value_type, Dimensions>;
using const_iterator =
typename detail::accessor_iterator<const DataT, Dimensions>;
typename detail::accessor_iterator<const value_type, Dimensions>;
using difference_type =
typename std::iterator_traits<iterator>::difference_type;

Expand Down
57 changes: 57 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,57 @@
// 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 AccessorT =
typename sycl::accessor<DataT, Dimensions, AccessMode, AccessTarget>;
static_assert(std::is_same_v<sycl::detail::accessor_iterator<
typename AccessorT::value_type, Dimensions>,
typename AccessorT::iterator>);

static_assert(
std::is_same_v<sycl::detail::accessor_iterator<
const typename AccessorT::value_type, Dimensions>,
typename AccessorT::const_iterator>);
}

template <typename DataT, int Dimensions, sycl::access_mode AccessMode>
void check_host_accessor() {
using AccessorT = typename sycl::host_accessor<DataT, Dimensions, AccessMode>;
static_assert(std::is_same_v<sycl::detail::accessor_iterator<
typename AccessorT::value_type, Dimensions>,
typename AccessorT::iterator>);

static_assert(
std::is_same_v<sycl::detail::accessor_iterator<
const typename AccessorT::value_type, Dimensions>,
typename AccessorT::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;
}

0 comments on commit 227614c

Please sign in to comment.