Skip to content

[libc++] Replace __is_trivially_relocatable by is_trivially_copyable #124970

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

Merged
merged 7 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions libcxx/include/__type_traits/is_trivially_relocatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <__config>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_copyable.h>

Expand All @@ -23,8 +22,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD

// A type is trivially relocatable if a move construct + destroy of the original object is equivalent to
// `memcpy(dst, src, sizeof(T))`.

#if __has_builtin(__is_trivially_relocatable)
//
// Note that we don't use the __is_trivially_relocatable Clang builtin right now because it does not
// implement the semantics of any current or future trivial relocation proposal and it can lead to
// incorrect optimizations on some platforms (Windows) and supported compilers (AppleClang).
#if __has_builtin(__is_trivially_relocatable) && 0
template <class _Tp, class = void>
struct __libcpp_is_trivially_relocatable : integral_constant<bool, __is_trivially_relocatable(_Tp)> {};
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ struct MoveOnlyTriviallyCopyable {
MoveOnlyTriviallyCopyable(MoveOnlyTriviallyCopyable&&) = default;
MoveOnlyTriviallyCopyable& operator=(MoveOnlyTriviallyCopyable&&) = default;
};
#ifndef _MSC_VER
static_assert(std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "");
#else
static_assert(!std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "");
#endif

struct NonTrivialMoveConstructor {
NonTrivialMoveConstructor(NonTrivialMoveConstructor&&);
};
static_assert(!std::__libcpp_is_trivially_relocatable<NonTrivialMoveConstructor>::value, "");

struct NonTrivialDestructor {
~NonTrivialDestructor() {}
};
static_assert(!std::__libcpp_is_trivially_relocatable<NonTrivialDestructor>::value, "");

// library-internal types
// ----------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <vector>

// Make sure we don't miscompile vector operations for types that shouldn't be considered
// trivially relocatable.

#include <vector>
#include <cassert>
#include <cstddef>
#include <type_traits>
#include <utility>

#include "test_macros.h"

struct Tracker {
std::size_t move_constructs = 0;
};

struct [[clang::trivial_abi]] Inner {
TEST_CONSTEXPR_CXX20 explicit Inner(Tracker* tracker) : tracker_(tracker) {}
TEST_CONSTEXPR_CXX20 Inner(const Inner& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; }
TEST_CONSTEXPR_CXX20 Inner(Inner&& rhs) : tracker_(rhs.tracker_) { tracker_->move_constructs += 1; }
Tracker* tracker_;
};

// Even though this type contains a trivial_abi type, it is not trivially move-constructible,
// so we should not attempt to optimize its move construction + destroy using trivial relocation.
struct NotTriviallyMovable {
TEST_CONSTEXPR_CXX20 explicit NotTriviallyMovable(Tracker* tracker) : inner_(tracker) {}
TEST_CONSTEXPR_CXX20 NotTriviallyMovable(NotTriviallyMovable&& other) : inner_(std::move(other.inner_)) {}
Inner inner_;
};
static_assert(!std::is_trivially_copyable<NotTriviallyMovable>::value, "");
LIBCPP_STATIC_ASSERT(!std::__libcpp_is_trivially_relocatable<NotTriviallyMovable>::value, "");

TEST_CONSTEXPR_CXX20 bool tests() {
Tracker track;
std::vector<NotTriviallyMovable> v;

// Fill the vector at its capacity, such that any subsequent push_back would require growing.
v.reserve(5);
std::size_t const capacity = v.capacity(); // could technically be more than 5
while (v.size() < v.capacity()) {
v.emplace_back(&track);
}
assert(track.move_constructs == 0);
assert(v.capacity() == capacity);
assert(v.size() == capacity);

// Force a reallocation of the buffer + relocalization of the elements.
// All the existing elements of the vector should be move-constructed to their new location.
v.emplace_back(&track);
assert(track.move_constructs == capacity);

return true;
}

int main(int, char**) {
tests();
#if TEST_STD_VER >= 20
static_assert(tests());
#endif
return 0;
}