Revert "Use std::is_trivially_copyable", breaks MSVC build

Revert "Delete llvm::is_trivially_copyable and CMake variable HAVE_STD_IS_TRIVIALLY_COPYABLE"

This reverts commit 4d4bd40b57.

This reverts commit 557b00e0af.
This commit is contained in:
Reid Kleckner 2020-12-02 13:48:05 -08:00
parent 2304528bb5
commit 91e66bfd32
22 changed files with 144 additions and 38 deletions

View File

@ -351,6 +351,15 @@ else()
unset(HAVE_FFI_CALL CACHE)
endif( LLVM_ENABLE_FFI )
# Whether we can use std::is_trivially_copyable to verify llvm::is_trivially_copyable.
CHECK_CXX_SOURCE_COMPILES("
#include <type_traits>
struct T { int val; };
static_assert(std::is_trivially_copyable<T>::value, \"ok\");
int main() { return 0;}
" HAVE_STD_IS_TRIVIALLY_COPYABLE)
# Define LLVM_HAS_ATOMICS if gcc or MSVC atomic builtins are supported.
include(CheckAtomic)

View File

@ -1530,7 +1530,7 @@ SmallVector has grown a few other minor advantages over std::vector, causing
#. std::vector is exception-safe, and some implementations have pessimizations
that copy elements when SmallVector would move them.
#. SmallVector understands ``std::is_trivially_copyable<Type>`` and uses realloc aggressively.
#. SmallVector understands ``llvm::is_trivially_copyable<Type>`` and uses realloc aggressively.
#. Many LLVM APIs take a SmallVectorImpl as an out parameter (see the note
below).

View File

@ -426,8 +426,8 @@ protected:
setNumEntries(other.getNumEntries());
setNumTombstones(other.getNumTombstones());
if (std::is_trivially_copyable<KeyT>::value &&
std::is_trivially_copyable<ValueT>::value)
if (is_trivially_copyable<KeyT>::value &&
is_trivially_copyable<ValueT>::value)
memcpy(reinterpret_cast<void *>(getBuckets()), other.getBuckets(),
getNumBuckets() * sizeof(BucketT));
else

View File

@ -17,10 +17,10 @@
#include "llvm/ADT/None.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/type_traits.h"
#include <cassert>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
namespace llvm {
@ -32,7 +32,7 @@ namespace optional_detail {
struct in_place_t {};
/// Storage for any type.
template <typename T, bool = std::is_trivially_copyable<T>::value>
template <typename T, bool = is_trivially_copyable<T>::value>
class OptionalStorage {
union {
char empty;

View File

@ -15,6 +15,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/type_traits.h"
#include <cassert>
#include <cstdint>
#include <limits>
@ -126,6 +127,19 @@ public:
}
};
// Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable
// when compiled with gcc 4.9.
template <typename PointerTy, unsigned IntBits, typename IntType,
typename PtrTraits,
typename Info>
struct is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> : std::true_type {
#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
static_assert(std::is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>::value,
"inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
#endif
};
template <typename PointerT, unsigned IntBits, typename PtrTraits>
struct PointerIntPairInfo {
static_assert(PtrTraits::NumLowBitsAvailable <

View File

@ -1428,7 +1428,7 @@ template <typename T>
// is trivially copyable.
using sort_trivially_copyable = conjunction<
std::is_pointer<T>,
std::is_trivially_copyable<typename std::iterator_traits<T>::value_type>>;
is_trivially_copyable<typename std::iterator_traits<T>::value_type>>;
} // namespace detail
// Provide wrappers to std::sort which shuffle the elements before sorting

View File

@ -332,6 +332,9 @@
/* Define as the return type of signal handlers (`int' or `void'). */
#cmakedefine RETSIGTYPE ${RETSIGTYPE}
/* Define if std::is_trivially_copyable is supported */
#cmakedefine HAVE_STD_IS_TRIVIALLY_COPYABLE ${HAVE_STD_IS_TRIVIALLY_COPYABLE}
/* Define to a function implementing stricmp */
#cmakedefine stricmp ${stricmp}

View File

@ -171,10 +171,15 @@ struct GloballyHashedType {
return Hashes;
}
};
#if defined(_MSC_VER)
// is_trivially_copyable is not available in older versions of libc++, but it is
// available in all supported versions of MSVC, so at least this gives us some
// coverage.
static_assert(std::is_trivially_copyable<GloballyHashedType>::value,
"GloballyHashedType must be trivially copyable so that we can "
"reinterpret_cast arrays of hash data to arrays of "
"GloballyHashedType");
#endif
} // namespace codeview
template <> struct DenseMapInfo<codeview::LocallyHashedType> {

View File

@ -85,6 +85,11 @@ template<typename T> union move_construction_triviality_helper {
~move_construction_triviality_helper() = default;
};
template<class T>
union trivial_helper {
T t;
};
} // end namespace detail
/// An implementation of `std::is_trivially_copy_constructible` since we have
@ -109,6 +114,78 @@ struct is_trivially_move_constructible<T &> : std::true_type {};
template <typename T>
struct is_trivially_move_constructible<T &&> : std::true_type {};
template <typename T>
struct is_copy_assignable {
template<class F>
static auto get(F*) -> decltype(std::declval<F &>() = std::declval<const F &>(), std::true_type{});
static std::false_type get(...);
static constexpr bool value = decltype(get((T*)nullptr))::value;
};
template <typename T>
struct is_move_assignable {
template<class F>
static auto get(F*) -> decltype(std::declval<F &>() = std::declval<F &&>(), std::true_type{});
static std::false_type get(...);
static constexpr bool value = decltype(get((T*)nullptr))::value;
};
// An implementation of `std::is_trivially_copyable` since STL version
// is not equally supported by all compilers, especially GCC 4.9.
// Uniform implementation of this trait is important for ABI compatibility
// as it has an impact on SmallVector's ABI (among others).
template <typename T>
class is_trivially_copyable {
// copy constructors
static constexpr bool has_trivial_copy_constructor =
std::is_copy_constructible<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_copy_constructor =
!std::is_copy_constructible<T>::value;
// move constructors
static constexpr bool has_trivial_move_constructor =
std::is_move_constructible<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_move_constructor =
!std::is_move_constructible<T>::value;
// copy assign
static constexpr bool has_trivial_copy_assign =
is_copy_assignable<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_copy_assign =
!is_copy_assignable<T>::value;
// move assign
static constexpr bool has_trivial_move_assign =
is_move_assignable<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_move_assign =
!is_move_assignable<T>::value;
// destructor
static constexpr bool has_trivial_destructor =
std::is_destructible<detail::trivial_helper<T>>::value;
public:
static constexpr bool value =
has_trivial_destructor &&
(has_deleted_move_assign || has_trivial_move_assign) &&
(has_deleted_move_constructor || has_trivial_move_constructor) &&
(has_deleted_copy_assign || has_trivial_copy_assign) &&
(has_deleted_copy_constructor || has_trivial_copy_constructor);
#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
static_assert(value == std::is_trivially_copyable<T>::value,
"inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
#endif
};
template <typename T>
class is_trivially_copyable<T*> : public std::true_type {
};
} // end namespace llvm
#endif // LLVM_SUPPORT_TYPE_TRAITS_H

View File

@ -67,7 +67,7 @@ public:
unsigned NewSize = Storage.size() - 1;
if (NewSize) {
// Move the slot at the end to the beginning.
if (std::is_trivially_copyable<T>::value)
if (is_trivially_copyable<T>::value)
Storage[0] = Storage[NewSize];
else
std::swap(Storage[0], Storage[NewSize]);

View File

@ -262,7 +262,7 @@ TEST(ArrayRefTest, makeArrayRefFromStdArray) {
}
}
static_assert(std::is_trivially_copyable<ArrayRef<int>>::value,
static_assert(is_trivially_copyable<ArrayRef<int>>::value,
"trivially copyable");
} // end anonymous namespace

View File

@ -267,7 +267,7 @@ TEST_F(ImmutableListTest, LongListOrderingTest) {
ASSERT_EQ(6, i);
}
static_assert(std::is_trivially_copyable<ImmutableList<Wrapper<long>>>::value,
static_assert(is_trivially_copyable<ImmutableList<Wrapper<long>>>::value,
"trivially copyable");
} // namespace

View File

@ -17,10 +17,10 @@
using namespace llvm;
static_assert(std::is_trivially_copyable<Optional<int>>::value,
"trivially copyable");
static_assert(is_trivially_copyable<Optional<int>>::value,
"trivially copyable");
static_assert(std::is_trivially_copyable<Optional<std::array<int, 3>>>::value,
static_assert(is_trivially_copyable<Optional<std::array<int, 3>>>::value,
"trivially copyable");
void OptionalWorksInConstexpr() {
@ -66,8 +66,8 @@ unsigned NonDefaultConstructible::Destructions = 0;
unsigned NonDefaultConstructible::CopyAssignments = 0;
static_assert(
!std::is_trivially_copyable<Optional<NonDefaultConstructible>>::value,
"not trivially copyable");
!is_trivially_copyable<Optional<NonDefaultConstructible>>::value,
"not trivially copyable");
// Test fixture
class OptionalTest : public testing::Test {
@ -227,8 +227,9 @@ struct MultiArgConstructor {
};
unsigned MultiArgConstructor::Destructions = 0;
static_assert(!std::is_trivially_copyable<Optional<MultiArgConstructor>>::value,
"not trivially copyable");
static_assert(
!is_trivially_copyable<Optional<MultiArgConstructor>>::value,
"not trivially copyable");
TEST_F(OptionalTest, Emplace) {
MultiArgConstructor::ResetCounts();
@ -277,7 +278,7 @@ unsigned MoveOnly::MoveConstructions = 0;
unsigned MoveOnly::Destructions = 0;
unsigned MoveOnly::MoveAssignments = 0;
static_assert(!std::is_trivially_copyable<Optional<MoveOnly>>::value,
static_assert(!is_trivially_copyable<Optional<MoveOnly>>::value,
"not trivially copyable");
TEST_F(OptionalTest, MoveOnlyNull) {
@ -381,7 +382,7 @@ private:
unsigned Immovable::Constructions = 0;
unsigned Immovable::Destructions = 0;
static_assert(!std::is_trivially_copyable<Optional<Immovable>>::value,
static_assert(!is_trivially_copyable<Optional<Immovable>>::value,
"not trivially copyable");
TEST_F(OptionalTest, ImmovableEmplace) {

View File

@ -62,7 +62,7 @@ TEST(PointerIntPairTest, GetSet) {
EXPECT_EQ(&s, Pair2.getPointer());
EXPECT_EQ(E::Case3, Pair2.getInt());
static_assert(std::is_trivially_copyable<PointerIntPair<S *, 2, E>>::value,
static_assert(is_trivially_copyable<PointerIntPair<S *, 2, E>>::value,
"trivially copyable");
}
@ -101,7 +101,7 @@ TEST(PointerIntPairTest, ManyUnusedBits) {
(int)PointerLikeTypeTraits<decltype(pair)>::NumLowBitsAvailable);
static_assert(
std::is_trivially_copyable<
is_trivially_copyable<
PointerIntPair<Fixnum31, 1, bool, FixnumPointerTraits>>::value,
"trivially copyable");
}

View File

@ -1087,7 +1087,6 @@ TEST(StringRefTest, GTestPrinter) {
EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
}
static_assert(std::is_trivially_copyable<StringRef>::value,
"trivially copyable");
static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable");
} // end anonymous namespace

View File

@ -91,7 +91,7 @@ TEST_F(BlockFrequencyInfoTest, Basic) {
EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq);
}
static_assert(std::is_trivially_copyable<bfi_detail::BlockMass>::value,
static_assert(is_trivially_copyable<bfi_detail::BlockMass>::value,
"trivially copyable");
} // end anonymous namespace

View File

@ -161,7 +161,7 @@ TEST(BitstreamReaderTest, shortRead) {
}
}
static_assert(std::is_trivially_copyable<BitCodeAbbrevOp>::value,
static_assert(is_trivially_copyable<BitCodeAbbrevOp>::value,
"trivially copyable");
} // end anonymous namespace

View File

@ -383,7 +383,6 @@ TEST(MachineInstrExtraInfo, RemoveExtraInfo) {
ASSERT_FALSE(MI->getHeapAllocMarker());
}
static_assert(std::is_trivially_copyable<MCOperand>::value,
"trivially copyable");
static_assert(is_trivially_copyable<MCOperand>::value, "trivially copyable");
} // end namespace

View File

@ -12,15 +12,14 @@
#include "llvm/CodeGen/SlotIndexes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "gtest/gtest.h"
#include <type_traits>
using namespace llvm;
#if __has_feature(is_trivially_copyable) || (defined(__GNUC__) && __GNUC__ >= 5)
static_assert(std::is_trivially_copyable<PressureChange>::value, "trivially copyable");
static_assert(std::is_trivially_copyable<SDep>::value, "trivially copyable");
static_assert(std::is_trivially_copyable<SDValue>::value, "trivially copyable");
static_assert(std::is_trivially_copyable<SlotIndex>::value, "trivially copyable");
static_assert(std::is_trivially_copyable<IdentifyingPassPtr>::value, "trivially copyable");
static_assert(is_trivially_copyable<PressureChange>::value, "trivially copyable");
static_assert(is_trivially_copyable<SDep>::value, "trivially copyable");
static_assert(is_trivially_copyable<SDValue>::value, "trivially copyable");
static_assert(is_trivially_copyable<SlotIndex>::value, "trivially copyable");
static_assert(is_trivially_copyable<IdentifyingPassPtr>::value, "trivially copyable");
#endif

View File

@ -267,11 +267,10 @@ TEST(CFGBuilder, Rebuild) {
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("d")->getTerminator()));
}
static_assert(std::is_trivially_copyable<succ_iterator>::value,
static_assert(is_trivially_copyable<succ_iterator>::value,
"trivially copyable");
static_assert(std::is_trivially_copyable<const_succ_iterator>::value,
static_assert(is_trivially_copyable<const_succ_iterator>::value,
"trivially copyable");
static_assert(std::is_trivially_copyable<succ_range>::value,
"trivially copyable");
static_assert(std::is_trivially_copyable<const_succ_range>::value,
static_assert(is_trivially_copyable<succ_range>::value, "trivially copyable");
static_assert(is_trivially_copyable<const_succ_range>::value,
"trivially copyable");

View File

@ -562,7 +562,7 @@ TEST(ScaledNumberHelpersTest, toIntBug) {
EXPECT_EQ(1u, (n * n).toInt<uint32_t>());
}
static_assert(std::is_trivially_copyable<ScaledNumber<uint32_t>>::value,
static_assert(is_trivially_copyable<ScaledNumber<uint32_t>>::value,
"trivially copyable");
} // end namespace

View File

@ -89,6 +89,7 @@ write_cmake_config("config") {
"HAVE_LIBPSAPI=",
"HAVE_MALLCTL=",
"HAVE_SIGNAL_H=1",
"HAVE_STD_IS_TRIVIALLY_COPYABLE=1",
"HAVE_STRERROR=1",
"HAVE_SYS_STAT_H=1",
"HAVE_SYS_TYPES_H=1",