Skip to content

Commit

Permalink
Implement enable_view (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco authored Jan 3, 2024
1 parent 3a91dd2 commit b4d490b
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ set(files
__node_handle
__nullptr
__ranges/enable_borrowed_range.h
__ranges/enable_view.h
__split_buffer
__sso_allocator
__std_stream
Expand Down Expand Up @@ -453,6 +454,7 @@ set(files
ostream
queue
random
ranges
ratio
regex
scoped_allocator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___RANGES_ENABLE_VIEW_H
#define _LIBCUDACXX___RANGES_ENABLE_VIEW_H

#ifndef __cuda_std__
#include <__config>
#endif // __cuda_std__

#include "../__concepts/derived_from.h"
#include "../__concepts/same_as.h"
#include "../__type_traits/enable_if.h"
#include "../__type_traits/is_class.h"
#include "../__type_traits/remove_cv.h"
#include "../__type_traits/void_t.h"

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
_LIBCUDACXX_BEGIN_NAMESPACE_RANGES

#if _LIBCUDACXX_STD_VER > 14

struct view_base { };

_LIBCUDACXX_BEGIN_NAMESPACE_RANGES_ABI

#if _LIBCUDACXX_STD_VER > 17

template<class _Derived>
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
class view_interface;

#else

template<class _Derived, enable_if_t<is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>, int> = 0>
class view_interface;

#endif // _LIBCUDACXX_STD_VER < 17

_LIBCUDACXX_END_NAMESPACE_RANGES_ABI

_LIBCUDACXX_TEMPLATE(class _Op, class _Yp)
_LIBCUDACXX_REQUIRES(is_convertible_v<_Op*, view_interface<_Yp>*>)
_LIBCUDACXX_INLINE_VISIBILITY
void __is_derived_from_view_interface(const _Op*, const view_interface<_Yp>*);

#if _LIBCUDACXX_STD_VER > 17

template <class _Tp>
_LIBCUDACXX_INLINE_VAR constexpr bool enable_view = derived_from<_Tp, view_base> ||
requires { _CUDA_VRANGES::__is_derived_from_view_interface((_Tp*)nullptr, (_Tp*)nullptr); };

#else

template <class _Tp, class = void>
_LIBCUDACXX_INLINE_VAR constexpr bool enable_view = derived_from<_Tp, view_base>;

template <class _Tp>
_LIBCUDACXX_INLINE_VAR constexpr bool enable_view<_Tp,
void_t<decltype(_CUDA_VRANGES::__is_derived_from_view_interface((_Tp*)nullptr, (_Tp*)nullptr))>> = true;
#endif // _LIBCUDACXX_STD_VER < 17

#endif // _LIBCUDACXX_STD_VER > 14

_LIBCUDACXX_END_NAMESPACE_RANGES

#endif // _LIBCUDACXX___RANGES_ENABLE_VIEW_H
1 change: 1 addition & 0 deletions libcudacxx/include/cuda/std/detail/libcxx/include/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ namespace std {

#include "__assert" // all public C++ headers provide the assertion handler
#include "__ranges/enable_borrowed_range.h"
#include "__ranges/enable_view.h"

// standard-mandated includes
#include "version"
Expand Down
4 changes: 4 additions & 0 deletions libcudacxx/include/cuda/std/detail/libcxx/include/span
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ template<class R>
#include "__iterator/wrap_iter.h"
#include "__memory/pointer_traits.h"
#include "__ranges/enable_borrowed_range.h"
#include "__ranges/enable_view.h"
#include "__type_traits/enable_if.h"
#include "__type_traits/integral_constant.h"
#include "__type_traits/is_array.h"
Expand Down Expand Up @@ -581,6 +582,9 @@ _LIBCUDACXX_END_NAMESPACE_STD
_LIBCUDACXX_BEGIN_NAMESPACE_RANGES
template<class _Tp, size_t _Extent>
_LIBCUDACXX_INLINE_VAR constexpr bool enable_borrowed_range<span<_Tp, _Extent>> = true;

template <class _Tp, size_t _Extent>
_LIBCUDACXX_INLINE_VAR constexpr bool enable_view<span<_Tp, _Extent>> = true;
_LIBCUDACXX_END_NAMESPACE_RANGES
#endif // _CCCL_STD_VER > 2014

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: msvc-19.16

// span

#include <span>

#include <concepts>
#include <ranges>

using range = std::span<int>;

#ifdef _LIBCUDACXX_HAS_RANGES
static_assert(std::same_as<std::ranges::iterator_t<range>, range::iterator>);
static_assert(std::ranges::common_range<range>);
static_assert(std::ranges::random_access_range<range>);
static_assert(std::ranges::contiguous_range<range>);
static_assert(std::ranges::view<range> && std::ranges::enable_view<range>);
static_assert(std::ranges::sized_range<range>);
static_assert(std::ranges::borrowed_range<range>);
static_assert(std::ranges::viewable_range<range>);

static_assert(std::same_as<std::ranges::iterator_t<range const>, range::iterator>);
static_assert(std::ranges::common_range<range const>);
static_assert(std::ranges::random_access_range<range const>);
static_assert(std::ranges::contiguous_range<range const>);
static_assert(!std::ranges::view<range const> && !std::ranges::enable_view<range const>);
static_assert(std::ranges::sized_range<range const>);
static_assert(std::ranges::borrowed_range<range const>);
static_assert(std::ranges::viewable_range<range const>);
#endif // _LIBCUDACXX_HAS_RANGES

int main(int, char**)
{
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// 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
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: msvc-19.16

// span

#include <cuda/std/span>

#include <cuda/std/concepts>
#include <cuda/std/ranges>

using range = cuda::std::span<int>;

#ifdef _LIBCUDACXX_HAS_RANGES
static_assert(cuda::std::same_as<cuda::std::ranges::iterator_t<range>, range::iterator>);
static_assert(cuda::std::ranges::common_range<range>);
static_assert(cuda::std::ranges::random_access_range<range>);
static_assert(cuda::std::ranges::contiguous_range<range>);
static_assert(cuda::std::ranges::view<range> && cuda::std::ranges::enable_view<range>);
static_assert(cuda::std::ranges::sized_range<range>);
static_assert(cuda::std::ranges::borrowed_range<range>);
static_assert(cuda::std::ranges::viewable_range<range>);

static_assert(cuda::std::same_as<cuda::std::ranges::iterator_t<range const>, range::iterator>);
static_assert(cuda::std::ranges::common_range<range const>);
static_assert(cuda::std::ranges::random_access_range<range const>);
static_assert(cuda::std::ranges::contiguous_range<range const>);
static_assert(!cuda::std::ranges::view<range const> && !cuda::std::ranges::enable_view<range const>);
static_assert(cuda::std::ranges::sized_range<range const>);
static_assert(cuda::std::ranges::borrowed_range<range const>);
static_assert(cuda::std::ranges::viewable_range<range const>);
#endif // _LIBCUDACXX_HAS_RANGES

int main(int, char**)
{
return 0;
}

0 comments on commit b4d490b

Please sign in to comment.