Skip to content

Commit 6da2fc4

Browse files
committed
[libc++] Replace __is_trivially_relocatable by is_trivially_copyable
The __is_trivially_relocatable builtin has semantics that do not correspond to any current or future notion of trivial relocation. Furthermore, it currently leads to incorrect optimizations for some types on supported compilers: - Clang on Windows where types with non-trivial destructors get incorrectly optimized - AppleClang where types with non-trivial move constructors get incorrectly optimized Until there is an agreed upon and bugfree implementation of what it means to be trivially relocatable, it is safer to simply use trivially copyable instead. This doesn't leave a lot of types behind and is definitely correct.
1 parent f308af7 commit 6da2fc4

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

libcxx/include/__type_traits/is_trivially_relocatable.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <__config>
1313
#include <__type_traits/enable_if.h>
14-
#include <__type_traits/integral_constant.h>
1514
#include <__type_traits/is_same.h>
1615
#include <__type_traits/is_trivially_copyable.h>
1716

@@ -23,14 +22,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2322

2423
// A type is trivially relocatable if a move construct + destroy of the original object is equivalent to
2524
// `memcpy(dst, src, sizeof(T))`.
26-
27-
#if __has_builtin(__is_trivially_relocatable)
28-
template <class _Tp, class = void>
29-
struct __libcpp_is_trivially_relocatable : integral_constant<bool, __is_trivially_relocatable(_Tp)> {};
30-
#else
3125
template <class _Tp, class = void>
3226
struct __libcpp_is_trivially_relocatable : is_trivially_copyable<_Tp> {};
33-
#endif
3427

3528
template <class _Tp>
3629
struct __libcpp_is_trivially_relocatable<_Tp,

libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ static_assert(std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>:
6060
static_assert(!std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "");
6161
#endif
6262

63+
struct NonTrivialMoveConstructor {
64+
NonTrivialMoveConstructor(NonTrivialMoveConstructor&&);
65+
};
66+
static_assert(!std::__libcpp_is_trivially_relocatable<NonTrivialMoveConstructor>::value, "");
67+
68+
struct NonTrivialDestructor {
69+
~NonTrivialDestructor() {}
70+
};
71+
static_assert(!std::__libcpp_is_trivially_relocatable<NonTrivialDestructor>::value, "");
72+
6373
// library-internal types
6474
// ----------------------
6575

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <vector>
10+
11+
// Make sure we don't miscompile vector operations for types that shouldn't be considered
12+
// trivially relocatable.
13+
14+
#include <vector>
15+
#include <cassert>
16+
#include <cstddef>
17+
18+
#include "test_macros.h"
19+
20+
struct Tracker {
21+
std::size_t move_constructs = 0;
22+
};
23+
24+
struct [[clang::trivial_abi]] Inner {
25+
TEST_CONSTEXPR explicit Inner(Tracker* tracker) : tracker_(tracker) {}
26+
TEST_CONSTEXPR Inner(const Inner& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; }
27+
TEST_CONSTEXPR Inner(Inner&& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; }
28+
Tracker* tracker_;
29+
};
30+
31+
// Even though this type contains a trivial_abi type, it is not trivially move-constructible,
32+
// so we should not attempt to optimize its move construction + destroy using trivial relocation.
33+
struct NotTriviallyMovable {
34+
TEST_CONSTEXPR explicit NotTriviallyMovable(Tracker* tracker) : inner_(tracker) {}
35+
TEST_CONSTEXPR NotTriviallyMovable(NotTriviallyMovable&& other) : inner_(std::move(other.inner_)) {}
36+
Inner inner_;
37+
};
38+
static_assert(!std::is_trivially_copyable<NotTriviallyMovable>::value, "");
39+
LIBCPP_STATIC_ASSERT(!std::__libcpp_is_trivially_relocatable<NotTriviallyMovable>::value, "");
40+
41+
TEST_CONSTEXPR_CXX20 bool tests() {
42+
Tracker track;
43+
std::vector<NotTriviallyMovable> v;
44+
45+
// Fill the vector at its capacity, such that any subsequent push_back would require growing.
46+
v.reserve(5);
47+
for (std::size_t i = 0; i != 5; ++i) {
48+
v.emplace_back(&track);
49+
}
50+
assert(track.move_constructs == 0);
51+
assert(v.size() == 5);
52+
53+
// Force a reallocation of the buffer + relocalization of the elements.
54+
// All the existing elements of the vector should be move-constructed to their new location.
55+
v.emplace_back(&track);
56+
assert(track.move_constructs == 5);
57+
58+
return true;
59+
}
60+
61+
int main(int, char**) {
62+
tests();
63+
#if TEST_STD_VER >= 20
64+
static_assert(tests());
65+
#endif
66+
return 0;
67+
}

0 commit comments

Comments
 (0)