Skip to content

[SYCL] Add range and nd_range CTAD support #772

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

Merged
merged 1 commit into from
Nov 15, 2019
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
9 changes: 9 additions & 0 deletions sycl/include/CL/sycl/id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,14 @@ size_t getOffsetForId(range<dimensions> Range, id<dimensions> Id,
return offset;
}
} // namespace detail

// C++ feature test macros are supported by all supported compilers
// with the exception of MSVC 1914. It doesn't support deduction guides.
#ifdef __cpp_deduction_guides
id(size_t)->id<1>;
id(size_t, size_t)->id<2>;
id(size_t, size_t, size_t)->id<3>;
#endif

} // namespace sycl
} // namespace cl
8 changes: 4 additions & 4 deletions sycl/include/CL/sycl/nd_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ template <int dimensions = 1> class nd_range {
range<dimensions> globalSize;
range<dimensions> localSize;
id<dimensions> offset;
static_assert(dimensions >= 1 && dimensions <= 3,
"nd_range can only be 1, 2, or 3 dimensional.");

public:
template <int N = dimensions>
nd_range(
typename std::enable_if<((N > 0) && (N < 4)), range<dimensions>>::type globalSize,
range<dimensions> localSize, id<dimensions> offset = id<dimensions>())
nd_range(range<dimensions> globalSize, range<dimensions> localSize,
id<dimensions> offset = id<dimensions>())
: globalSize(globalSize), localSize(localSize), offset(offset) {}

range<dimensions> get_global_range() const { return globalSize; }
Expand Down
6 changes: 6 additions & 0 deletions sycl/include/CL/sycl/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,11 @@ template <int dimensions = 1> class range : public detail::array<dimensions> {
#undef __SYCL_GEN_OPT
};

#ifdef __cpp_deduction_guides
range(size_t)->range<1>;
range(size_t, size_t)->range<2>;
range(size_t, size_t, size_t)->range<3>;
#endif

} // namespace sycl
} // namespace cl
20 changes: 20 additions & 0 deletions sycl/test/basic_tests/id_ctad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %clangxx -std=c++17 -fsyntax-only -Xclang -verify %s
// expected-no-diagnostics
//==--------------- id_ctad.cpp - SYCL id CTAD 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 <CL/sycl.hpp>

using namespace std;
int main() {
cl::sycl::id one_dim_id(64);
cl::sycl::id two_dim_id(64, 1);
cl::sycl::id three_dim_id(64, 1, 2);
static_assert(std::is_same_v<decltype(one_dim_id), cl::sycl::id<1>>);
static_assert(std::is_same_v<decltype(two_dim_id), cl::sycl::id<2>>);
static_assert(std::is_same_v<decltype(three_dim_id), cl::sycl::id<3>>);
}
29 changes: 29 additions & 0 deletions sycl/test/basic_tests/range_ctad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %clangxx -std=c++17 -fsyntax-only -Xclang -verify %s
// expected-no-diagnostics
//==--------------- range_ctad.cpp - SYCL range CTAD 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 <CL/sycl.hpp>

using namespace std;
int main() {
cl::sycl::range one_dim_range(64);
cl::sycl::range two_dim_range(64, 1);
cl::sycl::range three_dim_range(64, 1, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting to combine new-style CTAD with old-style explicit constructor call...

static_assert(std::is_same_v<decltype(one_dim_range), cl::sycl::range<1>>);
static_assert(std::is_same_v<decltype(two_dim_range), cl::sycl::range<2>>);
static_assert(std::is_same_v<decltype(three_dim_range), cl::sycl::range<3>>);
cl::sycl::nd_range one_dim_ndrange(one_dim_range, one_dim_range);
cl::sycl::nd_range two_dim_ndrange(two_dim_range, two_dim_range);
cl::sycl::nd_range three_dim_ndrange(three_dim_range, three_dim_range);
static_assert(
std::is_same_v<decltype(one_dim_ndrange), cl::sycl::nd_range<1>>);
static_assert(
std::is_same_v<decltype(two_dim_ndrange), cl::sycl::nd_range<2>>);
static_assert(
std::is_same_v<decltype(three_dim_ndrange), cl::sycl::nd_range<3>>);
}