]> granicus.if.org Git - llvm/commitdiff
Make llvm::Optional<T> trivially copyable when T is trivially copyable
authorSerge Guelton <sguelton@redhat.com>
Wed, 13 Feb 2019 09:31:22 +0000 (09:31 +0000)
committerSerge Guelton <sguelton@redhat.com>
Wed, 13 Feb 2019 09:31:22 +0000 (09:31 +0000)
This is an ever-recurring issue (see https://bugs.llvm.org/show_bug.cgi?id=39427 and https://bugs.llvm.org/show_bug.cgi?id=35978)
but I believe that thanks to https://reviews.llvm.org/D54472 we can now ship a decent implementation of this.

Basically the fact that llvm::is_trivially_copyable has a consistent behavior across compilers should prevent any ABI issue,
and using in-place new instead of memcpy should keep compiler bugs away.

Differential Revision: https://reviews.llvm.org/D57097

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@353927 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/Optional.h
unittests/ADT/OptionalTest.cpp

index 25a3185064f413225f0c63db53ea87114657dd75..35ce0ce7010ee4e244134671f39bf0f021fbd247 100644 (file)
@@ -29,36 +29,77 @@ namespace llvm {
 class raw_ostream;
 
 namespace optional_detail {
+
 /// Storage for any type.
-template <typename T, bool = is_trivially_copyable<T>::value> struct OptionalStorage {
+//
+template <class T> struct OptionalTrivialStorage {
   AlignedCharArrayUnion<T> storage;
   bool hasVal = false;
+  OptionalTrivialStorage() = default;
+  OptionalTrivialStorage(OptionalTrivialStorage const &) = default;
+  OptionalTrivialStorage(OptionalTrivialStorage &&) = default;
+  OptionalTrivialStorage &operator=(OptionalTrivialStorage const &) = default;
+  OptionalTrivialStorage &operator=(OptionalTrivialStorage &&) = default;
+  ~OptionalTrivialStorage() = default;
+
+  OptionalTrivialStorage(const T &y) : hasVal(true) {
+    new (storage.buffer) T(y);
+  }
+  OptionalTrivialStorage(T &&y) : hasVal(true) {
+    new (storage.buffer) T(std::move(y));
+  }
 
-  OptionalStorage() = default;
+  OptionalTrivialStorage &operator=(const T &y) {
+    new (storage.buffer) T(y);
+    hasVal = true;
+    return *this;
+  }
+  OptionalTrivialStorage &operator=(T &&y) {
+    new (storage.buffer) T(std::move(y));
+    hasVal = true;
+    return *this;
+  }
 
-  OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
-  OptionalStorage(const OptionalStorage &O) : hasVal(O.hasVal) {
-    if (hasVal)
-      new (storage.buffer) T(*O.getPointer());
+  T *getPointer() {
+    assert(hasVal);
+    return reinterpret_cast<T *>(storage.buffer);
   }
-  OptionalStorage(T &&y) : hasVal(true) {
-    new (storage.buffer) T(std::forward<T>(y));
+  const T *getPointer() const {
+    assert(hasVal);
+    return reinterpret_cast<const T *>(storage.buffer);
   }
-  OptionalStorage(OptionalStorage &&O) : hasVal(O.hasVal) {
+  void reset() { hasVal = false; }
+};
+
+template <typename T> struct OptionalStorage : OptionalTrivialStorage<T> {
+
+  OptionalStorage() = default;
+
+  OptionalStorage(const T &y) : OptionalTrivialStorage<T>(y) {}
+  OptionalStorage(T &&y) : OptionalTrivialStorage<T>(std::move(y)) {}
+
+  OptionalStorage(const OptionalStorage &O) : OptionalTrivialStorage<T>() {
+    this->hasVal = O.hasVal;
+    if (this->hasVal)
+      new (this->storage.buffer) T(*O.getPointer());
+  }
+
+  OptionalStorage(OptionalStorage &&O) : OptionalTrivialStorage<T>() {
+    this->hasVal = O.hasVal;
     if (O.hasVal) {
-      new (storage.buffer) T(std::move(*O.getPointer()));
+      new (this->storage.buffer) T(std::move(*O.getPointer()));
     }
   }
 
   OptionalStorage &operator=(T &&y) {
-    if (hasVal)
-      *getPointer() = std::move(y);
+    if (this->hasVal)
+      *this->getPointer() = std::move(y);
     else {
-      new (storage.buffer) T(std::move(y));
-      hasVal = true;
+      OptionalTrivialStorage<T>::operator=(std::move(y));
     }
     return *this;
   }
+
   OptionalStorage &operator=(OptionalStorage &&O) {
     if (!O.hasVal)
       reset();
@@ -74,11 +115,10 @@ template <typename T, bool = is_trivially_copyable<T>::value> struct OptionalSto
   // requirements (notably: the existence of a default ctor) when implemented
   // in that way. Careful SFINAE to avoid such pitfalls would be required.
   OptionalStorage &operator=(const T &y) {
-    if (hasVal)
-      *getPointer() = y;
+    if (this->hasVal)
+      *this->getPointer() = y;
     else {
-      new (storage.buffer) T(y);
-      hasVal = true;
+      OptionalTrivialStorage<T>::operator=(y);
     }
     return *this;
   }
@@ -93,26 +133,19 @@ template <typename T, bool = is_trivially_copyable<T>::value> struct OptionalSto
   ~OptionalStorage() { reset(); }
 
   void reset() {
-    if (hasVal) {
-      (*getPointer()).~T();
-      hasVal = false;
+    if (this->hasVal) {
+      (*this->getPointer()).~T();
+      OptionalTrivialStorage<T>::reset();
     }
   }
-
-  T *getPointer() {
-    assert(hasVal);
-    return reinterpret_cast<T *>(storage.buffer);
-  }
-  const T *getPointer() const {
-    assert(hasVal);
-    return reinterpret_cast<const T *>(storage.buffer);
-  }
 };
 
 } // namespace optional_detail
 
 template <typename T> class Optional {
-  optional_detail::OptionalStorage<T> Storage;
+  typename std::conditional<is_trivially_copyable<T>::value,
+                            optional_detail::OptionalTrivialStorage<T>,
+                            optional_detail::OptionalStorage<T>>::type Storage;
 
 public:
   using value_type = T;
index 9d6d5d29fe001b7a5250fc494e1f83e68516d1fe..8d49b3dc22ac8a1967678c4bdac287b01cd26f3a 100644 (file)
@@ -16,6 +16,12 @@ using namespace llvm;
 
 namespace {
 
+static_assert(llvm::is_trivially_copyable<Optional<int>>::value,
+              "trivially copyable");
+
+static_assert(llvm::is_trivially_copyable<Optional<std::array<int, 3>>>::value,
+              "trivially copyable");
+
 struct NonDefaultConstructible {
   static unsigned CopyConstructions;
   static unsigned Destructions;
@@ -43,6 +49,10 @@ unsigned NonDefaultConstructible::CopyConstructions = 0;
 unsigned NonDefaultConstructible::Destructions = 0;
 unsigned NonDefaultConstructible::CopyAssignments = 0;
 
+static_assert(
+    !llvm::is_trivially_copyable<Optional<NonDefaultConstructible>>::value,
+    "not trivially copyable");
+
 // Test fixture
 class OptionalTest : public testing::Test {
 };
@@ -201,6 +211,10 @@ struct MultiArgConstructor {
 };
 unsigned MultiArgConstructor::Destructions = 0;
 
+static_assert(
+    !llvm::is_trivially_copyable<Optional<MultiArgConstructor>>::value,
+    "not trivially copyable");
+
 TEST_F(OptionalTest, Emplace) {
   MultiArgConstructor::ResetCounts();
   Optional<MultiArgConstructor> A;
@@ -248,6 +262,9 @@ unsigned MoveOnly::MoveConstructions = 0;
 unsigned MoveOnly::Destructions = 0;
 unsigned MoveOnly::MoveAssignments = 0;
 
+static_assert(!llvm::is_trivially_copyable<Optional<MoveOnly>>::value,
+              "not trivially copyable");
+
 TEST_F(OptionalTest, MoveOnlyNull) {
   MoveOnly::ResetCounts();
   Optional<MoveOnly> O;
@@ -349,6 +366,9 @@ private:
 unsigned Immovable::Constructions = 0;
 unsigned Immovable::Destructions = 0;
 
+static_assert(!llvm::is_trivially_copyable<Optional<Immovable>>::value,
+              "not trivially copyable");
+
 TEST_F(OptionalTest, ImmovableEmplace) {
   Optional<Immovable> A;
   Immovable::ResetCounts();