[llvm] r342966 - Revert r342637 "[ADT] Try again to use the same version of llvm::Optional on all compilers"

Hans Wennborg via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 25 05:08:56 PDT 2018


Author: hans
Date: Tue Sep 25 05:08:56 2018
New Revision: 342966

URL: http://llvm.org/viewvc/llvm-project?rev=342966&view=rev
Log:
Revert r342637 "[ADT] Try again to use the same version of llvm::Optional on all compilers"

and also revert follow-ups r342643 and r342723.

This caused Clang to be miscompiled by GCC 4.8.4 (Unbuntu 14.04's
default compiler) and break the Chromium build (see
https://crbug.com/888061).

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=342966&r1=342965&r2=342966&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Optional.h (original)
+++ llvm/trunk/include/llvm/ADT/Optional.h Tue Sep 25 05:08:56 2018
@@ -22,7 +22,6 @@
 #include "llvm/Support/type_traits.h"
 #include <algorithm>
 #include <cassert>
-#include <cstring>
 #include <new>
 #include <utility>
 
@@ -109,6 +108,7 @@ 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;
@@ -116,24 +116,20 @@ template <typename T> struct OptionalSto
 
   OptionalStorage() = default;
 
-  OptionalStorage(const T &y) : hasVal(true) {
-    std::memmove(storage.buffer, &y, sizeof(y));
-  }
+  OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
   OptionalStorage &operator=(const T &y) {
-    std::memmove(storage.buffer, &y, sizeof(y));
+    *reinterpret_cast<T *>(storage.buffer) = y;
     hasVal = true;
     return *this;
   }
 
   void reset() { hasVal = false; }
 };
+#endif
 } // namespace optional_detail
 
 template <typename T> class Optional {
-  // GCC 5.4 miscompiles the trivially copyable OptionalStorage. Blacklist it.
-  optional_detail::OptionalStorage<T, isPodLike<T>::value &&
-                                          !std::is_enum<T>::value>
-      Storage;
+  optional_detail::OptionalStorage<T, isPodLike<T>::value> Storage;
 
 public:
   using value_type = T;




More information about the llvm-commits mailing list