-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[libc++] P2590R2: Explicit lifetime management #107206
Open
phyBrackets
wants to merge
3
commits into
llvm:main
Choose a base branch
from
phyBrackets:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// -*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___MEMORY_START_LIFETIME_AS_H | ||
#define _LIBCPP___MEMORY_START_LIFETIME_AS_H | ||
|
||
#include <__config> | ||
#include <__type_traits/is_array.h> | ||
#include <__type_traits/remove_all_extents.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
template <class _Tp> | ||
struct _LIBCPP_TEMPLATE_VIS __start_lifetime_as_impl { | ||
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR _Tp* __call(void* __p) _NOEXCEPT { return static_cast<_Tp*>(__p); } | ||
}; | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* start_lifetime_as(void* __p) _NOEXCEPT { | ||
return __start_lifetime_as_impl<_Tp>::__call(__p); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const _Tp* start_lifetime_as(const void* __p) _NOEXCEPT { | ||
return std::start_lifetime_as<_Tp>(const_cast<void*>(__p)); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR volatile _Tp* start_lifetime_as(volatile void* __p) _NOEXCEPT { | ||
return std::start_lifetime_as<_Tp>(const_cast<void*>(__p)); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const volatile _Tp* start_lifetime_as(const volatile void* __p) _NOEXCEPT { | ||
return std::start_lifetime_as<_Tp>(const_cast<void*>(__p)); | ||
} | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___MEMORY_START_LIFETIME_AS_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// -*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___MEMORY_START_LIFETIME_AS_ARRAY_H | ||
#define _LIBCPP___MEMORY_START_LIFETIME_AS_ARRAY_H | ||
|
||
#include <__config> | ||
#include <__memory/start_lifetime_as.h> | ||
#include <cstddef> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* | ||
start_lifetime_as_array(void* __p, [[__maybe_unused__]] size_t __n) _NOEXCEPT { | ||
return static_cast<_Tp*>(__p); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const _Tp* start_lifetime_as_array(const void* __p, size_t __n) _NOEXCEPT { | ||
return std::start_lifetime_as_array<_Tp>(const_cast<void*>(__p), __n); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR volatile _Tp* | ||
start_lifetime_as_array(volatile void* __p, size_t __n) _NOEXCEPT { | ||
return std::start_lifetime_as_array<_Tp>(const_cast<void*>(__p), __n); | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const volatile _Tp* | ||
start_lifetime_as_array(const volatile void* __p, size_t __n) _NOEXCEPT { | ||
return std::start_lifetime_as_array<_Tp>(const_cast<void*>(__p), __n); | ||
} | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___MEMORY_START_LIFETIME_AS_ARRAY_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
127 changes: 127 additions & 0 deletions
127
libcxx/test/std/utilities/memory/default.allocator/start_lifetime_as.pass.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,127 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <memory> | ||
|
||
// template <class T> | ||
// T* start_lifetime_as(void* p) noexcept; | ||
// | ||
// template <class T> | ||
// const T* start_lifetime_as(const void* p) noexcept; | ||
// | ||
// template <class T> | ||
// volatile T* start_lifetime_as(volatile void* p) noexcept; | ||
// | ||
// template <class T> | ||
// const volatile T* start_lifetime_as(const volatile void* p) noexcept; | ||
// | ||
// template <class T> | ||
// T* start_lifetime_as_array(void* p, size_t n) noexcept; | ||
// | ||
// template <class T> | ||
// const T* start_lifetime_as_array(const void* p, size_t n) noexcept; | ||
// | ||
// template <class T> | ||
// volatile T* start_lifetime_as_array(volatile void* p, size_t n) noexcept; | ||
// | ||
// template <class T> | ||
// const volatile T* start_lifetime_as_array(const volatile void* p, size_t n) noexcept; | ||
|
||
#include <memory> | ||
#include <type_traits> | ||
#include <cassert> | ||
|
||
#include "test_macros.h" | ||
|
||
struct S { | ||
int x; | ||
double y; | ||
}; | ||
|
||
template <class T> | ||
void test_start_lifetime_as() { | ||
alignas(T) char buffer[sizeof(T)]; | ||
|
||
{ | ||
T* ptr = std::start_lifetime_as<T>(buffer); | ||
ASSERT_SAME_TYPE(decltype(ptr), T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as<T>(buffer)); | ||
assert(ptr == reinterpret_cast<T*>(buffer)); | ||
} | ||
|
||
{ | ||
const T* cptr = std::start_lifetime_as<T>(static_cast<const void*>(buffer)); | ||
ASSERT_SAME_TYPE(decltype(cptr), const T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as<T>(static_cast<const void*>(buffer))); | ||
assert(cptr == reinterpret_cast<const T*>(buffer)); | ||
} | ||
|
||
{ | ||
volatile T* vptr = std::start_lifetime_as<T>(static_cast<volatile void*>(buffer)); | ||
ASSERT_SAME_TYPE(decltype(vptr), volatile T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as<T>(static_cast<volatile void*>(buffer))); | ||
assert(vptr == reinterpret_cast<volatile T*>(buffer)); | ||
} | ||
|
||
{ | ||
const volatile T* cvptr = std::start_lifetime_as<T>(static_cast<const volatile void*>(buffer)); | ||
ASSERT_SAME_TYPE(decltype(cvptr), const volatile T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as<T>(static_cast<const volatile void*>(buffer))); | ||
assert(cvptr == reinterpret_cast<const volatile T*>(buffer)); | ||
} | ||
} | ||
|
||
template <class T> | ||
void test_start_lifetime_as_array() { | ||
constexpr size_t count = 5; | ||
alignas(T) char buffer[sizeof(T) * count]; | ||
|
||
{ | ||
T* ptr = std::start_lifetime_as_array<T>(buffer, count); | ||
ASSERT_SAME_TYPE(decltype(ptr), T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as_array<T>(buffer, count)); | ||
assert(ptr == reinterpret_cast<T*>(buffer)); | ||
} | ||
|
||
{ | ||
const T* cptr = std::start_lifetime_as_array<T>(static_cast<const void*>(buffer), count); | ||
ASSERT_SAME_TYPE(decltype(cptr), const T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as_array<T>(static_cast<const void*>(buffer), count)); | ||
assert(cptr == reinterpret_cast<const T*>(buffer)); | ||
} | ||
|
||
{ | ||
volatile T* vptr = std::start_lifetime_as_array<T>(static_cast<volatile void*>(buffer), count); | ||
ASSERT_SAME_TYPE(decltype(vptr), volatile T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as_array<T>(static_cast<volatile void*>(buffer), count)); | ||
assert(vptr == reinterpret_cast<volatile T*>(buffer)); | ||
} | ||
|
||
{ | ||
const volatile T* cvptr = std::start_lifetime_as_array<T>(static_cast<const volatile void*>(buffer), count); | ||
ASSERT_SAME_TYPE(decltype(cvptr), const volatile T*); | ||
ASSERT_NOEXCEPT(std::start_lifetime_as_array<T>(static_cast<const volatile void*>(buffer), count)); | ||
assert(cvptr == reinterpret_cast<const volatile T*>(buffer)); | ||
} | ||
} | ||
|
||
int main(int, char**) { | ||
test_start_lifetime_as<char>(); | ||
test_start_lifetime_as<int>(); | ||
test_start_lifetime_as<double>(); | ||
test_start_lifetime_as<S>(); | ||
|
||
test_start_lifetime_as_array<char>(); | ||
test_start_lifetime_as_array<int>(); | ||
test_start_lifetime_as_array<double>(); | ||
test_start_lifetime_as_array<S>(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No built-in used here. Could you explain why this is considered to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function itself doesn't actually do anything at runtime. It's a no-op in terms of generated code. It happens at the compiler level. When the compiler sees a call to this function, it treats it as a signal that it should consider the lifetime of an array of
_Tp
objects to have begun at the memory location pointed to by__p
, even though no constructors have been called. Or is it intended something else? I am not sure, Could you explain?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, I'm not seeing anything in the source code of Clang or GCC that recognizes
start_lifetime_as(_array)
. So I can't believe this approach will automatically work,I guess an intrinsic like
__builitin_start_lifetime_as
(or__builtin_tbaa_barrier
?) is expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah Okay, that make sense, is it worth it if we currently define a macro to check if the compiler supports the necessary intrinsics?
If supported, it declares the intrinsics and implements
start_lifetime_as
andstart_lifetime_as_array
using these intrinsics. If not supported, it falls back to the current implementation (which is essentially a no-op).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a bad idea, since people expect things to work as advertised that exist. IOW, these functions should only exist if we can actually implement them.