Skip to content

Commit

Permalink
Revert "Use std::is_trivially_copyable", breaks MSVC build
Browse files Browse the repository at this point in the history
Revert "Delete llvm::is_trivially_copyable and CMake variable HAVE_STD_IS_TRIVIALLY_COPYABLE"

This reverts commit 4d4bd40.

This reverts commit 557b00e.
  • Loading branch information
rnk committed Dec 2, 2020
1 parent 2304528 commit 91e66bf
Show file tree
Hide file tree
Showing 22 changed files with 144 additions and 38 deletions.
9 changes: 9 additions & 0 deletions llvm/cmake/config-ix.cmake
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion llvm/docs/ProgrammersManual.rst
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/DenseMap.h
Expand Up @@ -426,8 +426,8 @@ class DenseMapBase : public DebugEpochBase {
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
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/Optional.h
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions llvm/include/llvm/ADT/PointerIntPair.h
Expand Up @@ -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>
Expand Down Expand Up @@ -126,6 +127,19 @@ class PointerIntPair {
}
};

// 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 <
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ADT/STLExtras.h
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Config/config.h.cmake
Expand Up @@ -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}

Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/DebugInfo/CodeView/TypeHashing.h
Expand Up @@ -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> {
Expand Down
77 changes: 77 additions & 0 deletions llvm/include/llvm/Support/type_traits.h
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion llvm/tools/llvm-diff/DifferenceEngine.cpp
Expand Up @@ -67,7 +67,7 @@ class PriorityQueue {
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]);
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/ADT/ArrayRefTest.cpp
Expand Up @@ -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
2 changes: 1 addition & 1 deletion llvm/unittests/ADT/ImmutableListTest.cpp
Expand Up @@ -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
19 changes: 10 additions & 9 deletions llvm/unittests/ADT/OptionalTest.cpp
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -381,7 +382,7 @@ struct Immovable {
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) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/unittests/ADT/PointerIntPairTest.cpp
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/unittests/ADT/StringRefTest.cpp
Expand Up @@ -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
2 changes: 1 addition & 1 deletion llvm/unittests/Analysis/BlockFrequencyInfoTest.cpp
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Bitstream/BitstreamReaderTest.cpp
Expand Up @@ -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
3 changes: 1 addition & 2 deletions llvm/unittests/CodeGen/MachineInstrTest.cpp
Expand Up @@ -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
11 changes: 5 additions & 6 deletions llvm/unittests/CodeGen/TypeTraitsTest.cpp
Expand Up @@ -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

9 changes: 4 additions & 5 deletions llvm/unittests/IR/CFGBuilder.cpp
Expand Up @@ -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");
2 changes: 1 addition & 1 deletion llvm/unittests/Support/ScaledNumberTest.cpp
Expand Up @@ -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

0 comments on commit 91e66bf

Please sign in to comment.