From fd015e4488fefe110be947cd9624de95d0d85ad7 Mon Sep 17 00:00:00 2001 From: Serge Guelton Date: Thu, 14 Feb 2019 19:17:00 +0000 Subject: [PATCH] Recommit Optional specialization for trivially copyable types Unfortunately the original code gets misscompiled by GCC (at least 8.1), this is a tentative workaround using std::memcpy instead of inplace new for trivially copyable types. I'll revert if it breaks. Original revision: https://reviews.llvm.org/D57097 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354051 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/Optional.h | 43 ++++++++++++++++++++++++++++++++++ unittests/ADT/OptionalTest.cpp | 20 ++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/include/llvm/ADT/Optional.h b/include/llvm/ADT/Optional.h index 25a3185064f..c628df9d711 100644 --- a/include/llvm/ADT/Optional.h +++ b/include/llvm/ADT/Optional.h @@ -21,6 +21,7 @@ #include "llvm/Support/type_traits.h" #include #include +#include #include #include @@ -109,6 +110,48 @@ template ::value> struct OptionalSto } }; +template struct OptionalStorage { + AlignedCharArrayUnion storage; + bool hasVal = false; + + OptionalStorage() = default; + + OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); } + OptionalStorage(const OptionalStorage &O) = default; + OptionalStorage(T &&y) : hasVal(true) { + std::memcpy(storage.buffer, reinterpret_cast(&y), sizeof(T)); + } + + OptionalStorage(OptionalStorage &&O) = default; + + OptionalStorage &operator=(T &&y) { + hasVal = true; + std::memcpy(storage.buffer, reinterpret_cast(&y), sizeof(T)); + return *this; + } + OptionalStorage &operator=(OptionalStorage &&O) = default; + + OptionalStorage &operator=(const T &y) { + hasVal = true; + std::memcpy(storage.buffer, reinterpret_cast(&y), sizeof(T)); + return *this; + } + OptionalStorage &operator=(const OptionalStorage &O) = default; + + ~OptionalStorage() = default; + + T *getPointer() { + assert(hasVal); + return reinterpret_cast(storage.buffer); + } + const T *getPointer() const { + assert(hasVal); + return reinterpret_cast(storage.buffer); + } + + void reset() { hasVal = false; } +}; + } // namespace optional_detail template class Optional { diff --git a/unittests/ADT/OptionalTest.cpp b/unittests/ADT/OptionalTest.cpp index 98adaccca96..c39b6727cd5 100644 --- a/unittests/ADT/OptionalTest.cpp +++ b/unittests/ADT/OptionalTest.cpp @@ -18,6 +18,12 @@ using namespace llvm; namespace { +static_assert(llvm::is_trivially_copyable>::value, + "trivially copyable"); + +static_assert(llvm::is_trivially_copyable>>::value, + "trivially copyable"); + struct NonDefaultConstructible { static unsigned CopyConstructions; static unsigned Destructions; @@ -45,6 +51,10 @@ unsigned NonDefaultConstructible::CopyConstructions = 0; unsigned NonDefaultConstructible::Destructions = 0; unsigned NonDefaultConstructible::CopyAssignments = 0; +static_assert( + !llvm::is_trivially_copyable>::value, + "not trivially copyable"); + // Test fixture class OptionalTest : public testing::Test { }; @@ -203,6 +213,10 @@ struct MultiArgConstructor { }; unsigned MultiArgConstructor::Destructions = 0; +static_assert( + !llvm::is_trivially_copyable>::value, + "not trivially copyable"); + TEST_F(OptionalTest, Emplace) { MultiArgConstructor::ResetCounts(); Optional A; @@ -250,6 +264,9 @@ unsigned MoveOnly::MoveConstructions = 0; unsigned MoveOnly::Destructions = 0; unsigned MoveOnly::MoveAssignments = 0; +static_assert(!llvm::is_trivially_copyable>::value, + "not trivially copyable"); + TEST_F(OptionalTest, MoveOnlyNull) { MoveOnly::ResetCounts(); Optional O; @@ -351,6 +368,9 @@ private: unsigned Immovable::Constructions = 0; unsigned Immovable::Destructions = 0; +static_assert(!llvm::is_trivially_copyable>::value, + "not trivially copyable"); + TEST_F(OptionalTest, ImmovableEmplace) { Optional A; Immovable::ResetCounts(); -- 2.50.1