-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose parts of
<cuda/std/memory>
(#2502)
This exposes some parts of `<memory>` that are frequently used and safe to use everywhere. We do not expose some features like allocators and smart pointers until we are sure that they are usefull and properly implemented. Co-authored-by: Michael Schellenberger Costa <[email protected]>
- Loading branch information
Showing
29 changed files
with
329 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.. _libcudacxx-standard-api-utility-memory: | ||
|
||
<cuda/std/memory> | ||
=================== | ||
|
||
Provided functionalities | ||
------------------------ | ||
|
||
- ``cuda::std::addressof``. See the C++ documentation of `std::addressof <https://en.cppreference.com/w/cpp/memory/addressof>`_ | ||
- ``cuda::std::align``. See the C++ documentation of `std::align <https://en.cppreference.com/w/cpp/memory/align>`_ | ||
- ``cuda::std::assume_aligned``. See the C++ documentation of `std::assume_aligned <https://en.cppreference.com/w/cpp/memory/assume_aligned>`_ | ||
- Uninitialized memory algorithms. See the C++ documentation `<https://en.cppreference.com/w/cpp/memory>`_ | ||
|
||
Extensions | ||
---------- | ||
|
||
- Most features are available from C++11 onwards. | ||
- ``cuda::std::addressof`` is constexpr from C++11 on if compiler support is available | ||
- ``cuda::std::assume_aligned`` is constexpr from C++14 on | ||
|
||
Restrictions | ||
------------ | ||
|
||
- `construct_at` and is only available in C++20 as that is explicitly mentioned in the standard | ||
- The specialized memory algorithms are not parallel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// -*- 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_ASSUME_ALIGNED_H | ||
#define _LIBCUDACXX___MEMORY_ASSUME_ALIGNED_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/__bit/has_single_bit.h> | ||
#include <cuda/std/__type_traits/is_constant_evaluated.h> | ||
#include <cuda/std/cstddef> // size_t | ||
#include <cuda/std/cstdint> // uintptr_t | ||
|
||
_LIBCUDACXX_BEGIN_NAMESPACE_STD | ||
|
||
template <size_t _Align, class _Tp> | ||
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX14 _Tp* assume_aligned(_Tp* __ptr) noexcept | ||
{ | ||
static_assert(_CUDA_VSTD::has_single_bit(_Align), "std::assume_aligned requires the alignment to be a power of 2!"); | ||
#if defined(_CCCL_BUILTIN_IS_CONSTANT_EVALUATED) && defined(_CCCL_BUILTIN_ASSUME_ALIGNED) | ||
if (!_CCCL_BUILTIN_IS_CONSTANT_EVALUATED()) | ||
{ | ||
# if !defined(_CCCL_COMPILER_MSVC) // MSVC checks within the builtin | ||
_CCCL_ASSERT(reinterpret_cast<uintptr_t>(__ptr) % _Align == 0, "Alignment assumption is violated"); | ||
# endif // !_CCCL_COMPILER_MSVC | ||
return static_cast<_Tp*>(_CCCL_BUILTIN_ASSUME_ALIGNED(__ptr, _Align)); | ||
} | ||
else | ||
#endif // _CCCL_BUILTIN_IS_CONSTANT_EVALUATED && _CCCL_BUILTIN_ASSUME_ALIGNED | ||
{ | ||
return __ptr; | ||
} | ||
} | ||
|
||
_LIBCUDACXX_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCUDACXX___MEMORY_ASSUME_ALIGNED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 _CUDA_STD_MEMORY | ||
#define _CUDA_STD_MEMORY | ||
|
||
#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/addressof.h> | ||
#include <cuda/std/__memory/align.h> | ||
#include <cuda/std/__memory/assume_aligned.h> | ||
#include <cuda/std/__memory/construct_at.h> | ||
#include <cuda/std/__memory/pointer_traits.h> | ||
#include <cuda/std/__memory/uninitialized_algorithms.h> | ||
|
||
// standard-mandated includes | ||
#include <cuda/std/version> | ||
|
||
// [memory.syn] | ||
#ifndef _LIBCUDACXX_HAS_NO_SPACESHIP_OPERATOR | ||
# include <cuda/std/compare> | ||
#endif // !_LIBCUDACXX_HAS_NO_SPACESHIP_OPERATOR | ||
|
||
#endif // _CUDA_STD_MEMORY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
libcudacxx/test/libcudacxx/std/utilities/memory/ptr.align/assume_aligned.nodiscard.fail.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// #include <memory> | ||
|
||
// template<size_t N, class T> | ||
// [[nodiscard]] constexpr T* assume_aligned(T* ptr); | ||
|
||
// UNSUPPORTED: nvrtc | ||
// nvrtc currently compiles the test with a warning | ||
|
||
#include <cuda/std/memory> | ||
|
||
__host__ __device__ void f() | ||
{ | ||
int* p = nullptr; | ||
cuda::std::assume_aligned<4>(p); // expected-warning {{ignoring return value of function declared with 'nodiscard' | ||
// attribute}} | ||
} | ||
|
||
int main(int, char**) | ||
{ | ||
return 0; | ||
} |
Oops, something went wrong.