Skip to content

Commit

Permalink
Modularize <memory> (#1639)
Browse files Browse the repository at this point in the history
* Drop `auto_ptr`

* Drop `raw_storage_iterator`

* Move `temporary_buffer` into its own file

* Move `compressed_pair` into its own file

* Move `unique_ptr` into its own file

* Move `destruct_n` helper to its own file

* Drop `swap_allocator`

* Drop `temp_value`

* Move `builtin_new_allocator` to its own file

* Drop `noexcept_move_assign_container`

* Move uninitialized algorithms into their own file

* Drop `shared_ptr`

* Drop legacy pointer safety features

* Cleanup `<memory>`

* Fix allocator traits

* Fix merge issue

* Silence issues with `is_constant_evaluated()` not being used currently

* Some MSVC fixes

* Fix is_constant_evaluated() in unique ptr tests

* Try more fixes

* drop HIDE_FROM_ABI

* Disable test that ICEs icc

* Fix issues around device operator new

* Fix exception handling in uninitialized algorithm tests

* Fix uninitialized construct
  • Loading branch information
miscco authored Apr 30, 2024
1 parent edde723 commit 41301ce
Show file tree
Hide file tree
Showing 91 changed files with 9,901 additions and 4,265 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
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 41301ce

Please sign in to comment.