Skip to content

Commit

Permalink
Merge branch 'main' into format_core_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Apr 30, 2024
2 parents 91f7d0e + 41301ce commit bf3c49b
Show file tree
Hide file tree
Showing 94 changed files with 10,013 additions and 4,152 deletions.
120 changes: 120 additions & 0 deletions libcudacxx/include/cuda/std/__algorithm/unwrap_range.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// 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) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_UNWRAP_RANGE_H
#define _LIBCUDACXX___ALGORITHM_UNWRAP_RANGE_H

#include <cuda/std/detail/__config>

#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

#include <cuda/std/__algorithm/unwrap_iter.h>
#include <cuda/std/__concepts/constructible.h>
#include <cuda/std/__iterator/concepts.h>
#include <cuda/std/__iterator/next.h>
#include <cuda/std/__utility/declval.h>
#include <cuda/std/__utility/move.h>
#include <cuda/std/__utility/pair.h>

_LIBCUDACXX_BEGIN_NAMESPACE_STD

// __unwrap_range and __rewrap_range are used to unwrap ranges which may have different iterator and sentinel types.
// __unwrap_iter and __rewrap_iter don't work for this, because they assume that the iterator and sentinel have
// the same type. __unwrap_range tries to get two iterators and then forward to __unwrap_iter.

#if _CCCL_STD_VER >= 2020
template <class _Iter, class _Sent>
struct __unwrap_range_impl
{
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__unwrap(_Iter __first, _Sent __sent)
requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
{
auto __last = ranges::next(__first, __sent);
return pair{_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__first)),
_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__last))};
}

inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__unwrap(_Iter __first, _Sent __last)
{
return pair{_CUDA_VSTD::move(__first), _CUDA_VSTD::move(__last)};
}

inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__rewrap(_Iter __orig_iter, decltype(_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__orig_iter))) __iter)
requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
{
return _CUDA_VSTD::__rewrap_iter(_CUDA_VSTD::move(__orig_iter), _CUDA_VSTD::move(__iter));
}

inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__rewrap(const _Iter&, _Iter __iter)
requires(!(random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>) )
{
return __iter;
}
};

template <class _Iter>
struct __unwrap_range_impl<_Iter, _Iter>
{
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__unwrap(_Iter __first, _Iter __last)
{
return pair{_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__first)),
_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__last))};
}

inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto
__rewrap(_Iter __orig_iter, decltype(_CUDA_VSTD::__unwrap_iter(__orig_iter)) __iter)
{
return _CUDA_VSTD::__rewrap_iter(_CUDA_VSTD::move(__orig_iter), _CUDA_VSTD::move(__iter));
}
};

template <class _Iter, class _Sent>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr auto __unwrap_range(_Iter __first, _Sent __last)
{
return __unwrap_range_impl<_Iter, _Sent>::__unwrap(_CUDA_VSTD::move(__first), _CUDA_VSTD::move(__last));
}

template <class _Sent, class _Iter, class _Unwrapped>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr _Iter
__rewrap_range(_Iter __orig_iter, _Unwrapped __iter)
{
return __unwrap_range_impl<_Iter, _Sent>::__rewrap(_CUDA_VSTD::move(__orig_iter), _CUDA_VSTD::move(__iter));
}
#else // ^^^ C++20 ^^^ / vvv C++17 vvv
template <class _Iter, class _Unwrapped = decltype(_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::declval<_Iter>()))>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 pair<_Unwrapped, _Unwrapped>
__unwrap_range(_Iter __first, _Iter __last)
{
return _CUDA_VSTD::make_pair(
_CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__first)), _CUDA_VSTD::__unwrap_iter(_CUDA_VSTD::move(__last)));
}

template <class _Iter, class _Unwrapped>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 _Iter
__rewrap_range(_Iter __orig_iter, _Unwrapped __iter)
{
return _CUDA_VSTD::__rewrap_iter(_CUDA_VSTD::move(__orig_iter), _CUDA_VSTD::move(__iter));
}
#endif // _CCCL_STD_VER <= 2017

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_UNWRAP_RANGE_H
21 changes: 20 additions & 1 deletion libcudacxx/include/cuda/std/__expected/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <cuda/std/__type_traits/negation.h>
#include <cuda/std/__type_traits/remove_cv.h>
#include <cuda/std/__type_traits/remove_cvref.h>
#include <cuda/std/__utility/as_const.h>
#include <cuda/std/__utility/exception_guard.h>
#include <cuda/std/__utility/forward.h>
#include <cuda/std/__utility/in_place.h>
Expand Down Expand Up @@ -551,6 +552,8 @@ class expected

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr const _Tp& value() const&
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() const& requires is_copy_constructible_v<E>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(this->__union_.__unex_);
Expand All @@ -560,15 +563,21 @@ class expected

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr _Tp& value() &
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() & requires is_copy_constructible_v<E>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(this->__union_.__unex_);
__throw_bad_expected_access<_Err>(_CUDA_VSTD::as_const(this->__union_.__unex_));
}
return this->__union_.__val_;
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr const _Tp&& value() const&&
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() const&& requires is_copy_constructible_v<E>");
static_assert(_LIBCUDACXX_TRAIT(is_constructible, _Err, decltype(_CUDA_VSTD::move(error()))),
"expected::value() const&& requires is_constructible_v<E, decltype(_CUDA_VSTD::move(error()))>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(_CUDA_VSTD::move(this->__union_.__unex_));
Expand All @@ -578,6 +587,10 @@ class expected

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr _Tp&& value() &&
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() && requires is_copy_constructible_v<E>");
static_assert(_LIBCUDACXX_TRAIT(is_constructible, _Err, decltype(_CUDA_VSTD::move(error()))),
"expected::value() && requires is_constructible_v<E, decltype(_CUDA_VSTD::move(error()))>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(_CUDA_VSTD::move(this->__union_.__unex_));
Expand Down Expand Up @@ -1473,6 +1486,8 @@ class expected<void, _Err>

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr void value() const&
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() const& requires is_copy_constructible_v<E>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(this->__union_.__unex_);
Expand All @@ -1481,6 +1496,10 @@ class expected<void, _Err>

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr void value() &&
{
static_assert(_LIBCUDACXX_TRAIT(is_copy_constructible, _Err),
"expected::value() && requires is_copy_constructible_v<E>");
static_assert(_LIBCUDACXX_TRAIT(is_move_constructible, _Err),
"expected::value() && requires is_move_constructible_v<E>");
if (!this->__has_val_)
{
__throw_bad_expected_access<_Err>(_CUDA_VSTD::move(this->__union_.__unex_));
Expand Down
4 changes: 4 additions & 0 deletions libcudacxx/include/cuda/std/__functional/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <cuda/std/__functional/invoke.h>
#include <cuda/std/__functional/unary_function.h>
#include <cuda/std/__iterator/iterator_traits.h>
#include <cuda/std/__memory/allocator_destructor.h>
#include <cuda/std/__memory/allocator_traits.h>
#include <cuda/std/__memory/builtin_new_allocator.h>
#include <cuda/std/__memory/compressed_pair.h>
#include <cuda/std/__type_traits/conditional.h>
#include <cuda/std/__type_traits/decay.h>
#include <cuda/std/__type_traits/enable_if.h>
Expand Down
2 changes: 1 addition & 1 deletion libcudacxx/include/cuda/std/__memory/allocator_arg_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _LIBCUDACXX_INLINE_VAR constexpr allocator_arg_t allocator_arg = allocator_arg_t
template <class _Tp, class _Alloc, class... _Args>
struct __uses_alloc_ctor_imp
{
typedef _LIBCUDACXX_NODEBUG __remove_cvref_t<_Alloc> _RawAlloc;
typedef _LIBCUDACXX_NODEBUG_TYPE __remove_cvref_t<_Alloc> _RawAlloc;
static const bool __ua = uses_allocator<_Tp, _RawAlloc>::value;
static const bool __ic = is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
static const int value = __ua ? 2 - __ic : 0;
Expand Down
2 changes: 1 addition & 1 deletion libcudacxx/include/cuda/std/__memory/allocator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ struct _LIBCUDACXX_TEMPLATE_VIS allocator_traits
};

template <class _Traits, class _Tp>
using __rebind_alloc _LIBCUDACXX_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;
using __rebind_alloc _LIBCUDACXX_NODEBUG_TYPE = typename _Traits::template rebind_alloc<_Tp>;

template <class _Traits, class _Tp>
struct __rebind_alloc_helper
Expand Down
87 changes: 87 additions & 0 deletions libcudacxx/include/cuda/std/__memory/builtin_new_allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// 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) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___MEMORY_BUILTIN_NEW_ALLOCATOR_H
#define _LIBCUDACXX___MEMORY_BUILTIN_NEW_ALLOCATOR_H

#include <cuda/std/detail/__config>

#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

#include <cuda/std/__memory/unique_ptr.h>
#include <cuda/std/cstddef>
#include <cuda/std/detail/libcxx/include/new>

_LIBCUDACXX_BEGIN_NAMESPACE_STD

// __builtin_new_allocator -- A non-templated helper for allocating and
// deallocating memory using __builtin_operator_new and
// __builtin_operator_delete. It should be used in preference to
// `std::allocator<T>` to avoid additional instantiations.
struct __builtin_new_allocator
{
struct __builtin_new_deleter
{
typedef void* pointer_type;

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr explicit __builtin_new_deleter(
size_t __size, size_t __align) noexcept
: __size_(__size)
, __align_(__align)
{}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY void operator()(void* __p) const noexcept
{
_CUDA_VSTD::__libcpp_deallocate(__p, __size_, __align_);
}

private:
size_t __size_;
size_t __align_;
};

typedef unique_ptr<void, __builtin_new_deleter> __holder_t;

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static __holder_t __allocate_bytes(size_t __s, size_t __align)
{
return __holder_t(_CUDA_VSTD::__libcpp_allocate(__s, __align), __builtin_new_deleter(__s, __align));
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static void
__deallocate_bytes(void* __p, size_t __s, size_t __align) noexcept
{
_CUDA_VSTD::__libcpp_deallocate(__p, __s, __align);
}

template <class _Tp>
_LIBCUDACXX_NODEBUG_TYPE _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static __holder_t
__allocate_type(size_t __n)
{
return __allocate_bytes(__n * sizeof(_Tp), _LIBCUDACXX_ALIGNOF(_Tp));
}

template <class _Tp>
_LIBCUDACXX_NODEBUG_TYPE _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY static void
__deallocate_type(void* __p, size_t __n) noexcept
{
__deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCUDACXX_ALIGNOF(_Tp));
}
};

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___MEMORY_BUILTIN_NEW_ALLOCATOR_H
Loading

0 comments on commit bf3c49b

Please sign in to comment.