[llvm] r346990 - Revert "[ADT] Drop llvm::Optional clang-specific optmization for trivially copyable types"

Tom Stellard via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 15 12:27:12 PST 2018


Author: tstellar
Date: Thu Nov 15 12:27:11 2018
New Revision: 346990

URL: http://llvm.org/viewvc/llvm-project?rev=346990&view=rev
Log:
Revert "[ADT] Drop llvm::Optional clang-specific optmization for trivially copyable types"

This reverts commit r346985.

It looks like one of the unittests also needs to be updated, reverting while I investigate.

Modified:
    llvm/trunk/include/llvm/ADT/Optional.h

Modified: llvm/trunk/include/llvm/ADT/Optional.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Optional.h?rev=346990&r1=346989&r2=346990&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Optional.h (original)
+++ llvm/trunk/include/llvm/ADT/Optional.h Thu Nov 15 12:27:11 2018
@@ -108,6 +108,24 @@ template <typename T, bool IsPodLike> st
   }
 };
 
+#if !defined(__GNUC__) || defined(__clang__) // GCC up to GCC7 miscompiles this.
+/// Storage for trivially copyable types only.
+template <typename T> struct OptionalStorage<T, true> {
+  AlignedCharArrayUnion<T> storage;
+  bool hasVal = false;
+
+  OptionalStorage() = default;
+
+  OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
+  OptionalStorage &operator=(const T &y) {
+    *reinterpret_cast<T *>(storage.buffer) = y;
+    hasVal = true;
+    return *this;
+  }
+
+  void reset() { hasVal = false; }
+};
+#endif
 } // namespace optional_detail
 
 template <typename T> class Optional {




More information about the llvm-commits mailing list