[llvm-branch-commits] [clang] d2ed9d6 - Revert "ADT: Migrate users of AlignedCharArrayUnion to std::aligned_union_t, NFC"

Reid Kleckner via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 14 17:09:46 PST 2020


Author: Reid Kleckner
Date: 2020-12-14T17:04:06-08:00
New Revision: d2ed9d6b7ec6ae837aac0b2d2920c1e34b89d00b

URL: https://github.com/llvm/llvm-project/commit/d2ed9d6b7ec6ae837aac0b2d2920c1e34b89d00b
DIFF: https://github.com/llvm/llvm-project/commit/d2ed9d6b7ec6ae837aac0b2d2920c1e34b89d00b.diff

LOG: Revert "ADT: Migrate users of AlignedCharArrayUnion to std::aligned_union_t, NFC"

We determined that the MSVC implementation of std::aligned* isn't suited
to our needs. It doesn't support 16 byte alignment or higher, and it
doesn't really guarantee 8 byte alignment. See
https://github.com/microsoft/STL/issues/1533

Also reverts "ADT: Change AlignedCharArrayUnion to an alias of std::aligned_union_t, NFC"

Also reverts "ADT: Remove AlignedCharArrayUnion, NFC" to bring back
AlignedCharArrayUnion.

This reverts commit 4d8bf870a82765eb0d4fe53c82f796b957c05954.

This reverts commit d10f9863a5ac1cb681af07719650c44b48f289ce.

This reverts commit 4b5dc150b9862271720b3d56a3e723a55dd81838.

Added: 
    llvm/include/llvm/Support/AlignOf.h
    llvm/unittests/Support/AlignOfTest.cpp

Modified: 
    clang/include/clang/AST/APValue.h
    clang/include/clang/AST/ASTContext.h
    clang/include/clang/AST/ASTTypeTraits.h
    clang/include/clang/AST/ParentMapContext.h
    clang/include/clang/Frontend/PrecompiledPreamble.h
    clang/include/clang/Sema/Overload.h
    clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
    clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
    clang/lib/Lex/PPDirectives.cpp
    llvm/include/llvm/ADT/DenseMap.h
    llvm/include/llvm/ADT/IntervalMap.h
    llvm/include/llvm/CodeGen/DIE.h
    llvm/include/llvm/CodeGen/SelectionDAGNodes.h
    llvm/include/llvm/Support/Error.h
    llvm/include/llvm/Support/ErrorOr.h
    llvm/include/llvm/Support/JSON.h
    llvm/include/llvm/Support/TrailingObjects.h
    llvm/include/llvm/Support/YAMLTraits.h
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/unittests/Support/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h
index d6a33c13b0961..eded34808ad7e 100644
--- a/clang/include/clang/AST/APValue.h
+++ b/clang/include/clang/AST/APValue.h
@@ -20,7 +20,7 @@
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/PointerUnion.h"
-#include <type_traits>
+#include "llvm/Support/AlignOf.h"
 
 namespace clang {
   class AddrLabelExpr;
@@ -286,10 +286,9 @@ class APValue {
   struct MemberPointerData;
 
   // We ensure elsewhere that Data is big enough for LV and MemberPointerData.
-  typedef std::aligned_union_t<1, void *, APSInt, APFloat, ComplexAPSInt,
-                               ComplexAPFloat, Vec, Arr, StructData, UnionData,
-                               AddrLabelDiffData>
-      DataType;
+  typedef llvm::AlignedCharArrayUnion<void *, APSInt, APFloat, ComplexAPSInt,
+                                      ComplexAPFloat, Vec, Arr, StructData,
+                                      UnionData, AddrLabelDiffData> DataType;
   static const size_t DataSize = sizeof(DataType);
 
   DataType Data;

diff  --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 0a635875207d1..71f824b69bc85 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -57,6 +57,7 @@
 #include "llvm/ADT/TinyPtrVector.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/ADT/iterator_range.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Compiler.h"

diff  --git a/clang/include/clang/AST/ASTTypeTraits.h b/clang/include/clang/AST/ASTTypeTraits.h
index ea3da8d90c5b9..8791e41b9ac3c 100644
--- a/clang/include/clang/AST/ASTTypeTraits.h
+++ b/clang/include/clang/AST/ASTTypeTraits.h
@@ -22,7 +22,7 @@
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/DenseMapInfo.h"
-#include <type_traits>
+#include "llvm/Support/AlignOf.h"
 
 namespace llvm {
 
@@ -456,8 +456,9 @@ class DynTypedNode {
   /// \c QualTypes, \c NestedNameSpecifierLocs, \c TypeLocs,
   /// \c TemplateArguments and \c TemplateArgumentLocs on the other hand do not
   /// have storage or unique pointers and thus need to be stored by value.
-  std::aligned_union_t<1, const void *, TemplateArgument, TemplateArgumentLoc,
-                       NestedNameSpecifierLoc, QualType, TypeLoc>
+  llvm::AlignedCharArrayUnion<const void *, TemplateArgument,
+                              TemplateArgumentLoc, NestedNameSpecifierLoc,
+                              QualType, TypeLoc>
       Storage;
 };
 

diff  --git a/clang/include/clang/AST/ParentMapContext.h b/clang/include/clang/AST/ParentMapContext.h
index a15790605a3d5..a0412380a864e 100644
--- a/clang/include/clang/AST/ParentMapContext.h
+++ b/clang/include/clang/AST/ParentMapContext.h
@@ -89,7 +89,7 @@ class TraversalKindScope {
 /// Container for either a single DynTypedNode or for an ArrayRef to
 /// DynTypedNode. For use with ParentMap.
 class DynTypedNodeList {
-  std::aligned_union_t<1, DynTypedNode, ArrayRef<DynTypedNode>> Storage;
+  llvm::AlignedCharArrayUnion<DynTypedNode, ArrayRef<DynTypedNode>> Storage;
   bool IsSingleNode;
 
 public:

diff  --git a/clang/include/clang/Frontend/PrecompiledPreamble.h b/clang/include/clang/Frontend/PrecompiledPreamble.h
index 1f768310d0b28..cea39bfec95d6 100644
--- a/clang/include/clang/Frontend/PrecompiledPreamble.h
+++ b/clang/include/clang/Frontend/PrecompiledPreamble.h
@@ -17,6 +17,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/MD5.h"
 #include <cstddef>
 #include <memory>
@@ -197,7 +198,7 @@ class PrecompiledPreamble {
 
   private:
     Kind StorageKind = Kind::Empty;
-    std::aligned_union_t<1, TempPCHFile, InMemoryPreamble> Storage = {};
+    llvm::AlignedCharArrayUnion<TempPCHFile, InMemoryPreamble> Storage = {};
   };
 
   /// Data used to determine if a file used in the preamble has been changed.

diff  --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 3c87dfcbb5448..5be6a618711cd 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -31,6 +31,7 @@
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"

diff  --git a/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp b/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
index 401a0eba18548..176d6d6abf33b 100644
--- a/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
+++ b/clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
@@ -11,6 +11,7 @@
 
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Errno.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/Path.h"

diff  --git a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
index ba96289c3356e..25cbcf536388a 100644
--- a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
+++ b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp
@@ -14,6 +14,7 @@
 
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Errno.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/Path.h"

diff  --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 0280afdb891fe..d6b03d85913d7 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -39,6 +39,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Path.h"
 #include <algorithm>

diff  --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 7da347125c34a..9814a379db7e1 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/EpochTracker.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemAlloc.h"
@@ -900,7 +901,7 @@ class SmallDenseMap
 
   /// A "union" of an inline bucket array and the struct representing
   /// a large bucket. This union will be discriminated by the 'Small' bit.
-  std::aligned_union_t<1, BucketT[InlineBuckets], LargeRep> storage;
+  AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
 
 public:
   explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
@@ -1040,7 +1041,7 @@ class SmallDenseMap
 
     if (Small) {
       // First move the inline buckets into a temporary storage.
-      std::aligned_union_t<1, BucketT[InlineBuckets]> TmpStorage;
+      AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
       BucketT *TmpBegin = reinterpret_cast<BucketT *>(&TmpStorage);
       BucketT *TmpEnd = TmpBegin;
 

diff  --git a/llvm/include/llvm/ADT/IntervalMap.h b/llvm/include/llvm/ADT/IntervalMap.h
index 7c571db17ce33..0b6c7d6678076 100644
--- a/llvm/include/llvm/ADT/IntervalMap.h
+++ b/llvm/include/llvm/ADT/IntervalMap.h
@@ -101,6 +101,7 @@
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/bit.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/RecyclingAllocator.h"
 #include <algorithm>
@@ -108,7 +109,6 @@
 #include <cstdint>
 #include <iterator>
 #include <new>
-#include <type_traits>
 #include <utility>
 
 namespace llvm {
@@ -963,7 +963,7 @@ class IntervalMap {
 
 private:
   // The root data is either a RootLeaf or a RootBranchData instance.
-  std::aligned_union_t<1, RootLeaf, RootBranchData> data;
+  AlignedCharArrayUnion<RootLeaf, RootBranchData> data;
 
   // Tree height.
   // 0: Leaves in root.

diff  --git a/llvm/include/llvm/CodeGen/DIE.h b/llvm/include/llvm/CodeGen/DIE.h
index 13a11a77222d5..1667cf8cd06d1 100644
--- a/llvm/include/llvm/CodeGen/DIE.h
+++ b/llvm/include/llvm/CodeGen/DIE.h
@@ -22,6 +22,7 @@
 #include "llvm/ADT/iterator_range.h"
 #include "llvm/BinaryFormat/Dwarf.h"
 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Allocator.h"
 #include <cassert>
 #include <cstddef>
@@ -367,9 +368,9 @@ class DIEValue {
   ///
   /// All values that aren't standard layout (or are larger than 8 bytes)
   /// should be stored by reference instead of by value.
-  using ValTy = std::aligned_union_t<1, DIEInteger, DIEString, DIEExpr,
-                                     DIELabel, DIEDelta *, DIEEntry, DIEBlock *,
-                                     DIELoc *, DIELocList, DIEBaseTypeRef *>;
+  using ValTy = AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
+                                      DIEDelta *, DIEEntry, DIEBlock *,
+                                      DIELoc *, DIELocList, DIEBaseTypeRef *>;
 
   static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
                     sizeof(ValTy) <= sizeof(void *),

diff  --git a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
index aa81a31bf23ad..5926c52c51d93 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
@@ -38,6 +38,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Operator.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/AtomicOrdering.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -2634,9 +2635,10 @@ template <> struct GraphTraits<SDNode*> {
 ///
 /// This needs to be a union because the largest node 
diff ers on 32 bit systems
 /// with 4 and 8 byte pointer alignment, respectively.
-using LargestSDNode =
-    std::aligned_union_t<1, AtomicSDNode, TargetIndexSDNode, BlockAddressSDNode,
-                         GlobalAddressSDNode, PseudoProbeSDNode>;
+using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
+                                            BlockAddressSDNode,
+                                            GlobalAddressSDNode,
+                                            PseudoProbeSDNode>;
 
 /// The SDNode class with the greatest alignment requirement.
 using MostAlignedSDNode = GlobalAddressSDNode;

diff  --git a/llvm/include/llvm/Support/AlignOf.h b/llvm/include/llvm/Support/AlignOf.h
new file mode 100644
index 0000000000000..f9dcde4d4ff12
--- /dev/null
+++ b/llvm/include/llvm/Support/AlignOf.h
@@ -0,0 +1,33 @@
+//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the AlignedCharArrayUnion class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_ALIGNOF_H
+#define LLVM_SUPPORT_ALIGNOF_H
+
+#include <type_traits>
+
+namespace llvm {
+
+/// A suitably aligned and sized character array member which can hold elements
+/// of any type.
+///
+/// These types may be arrays, structs, or any other types. This exposes a
+/// `buffer` member which can be used as suitable storage for a placement new of
+/// any of these types.
+template <typename T, typename... Ts> struct AlignedCharArrayUnion {
+  using AlignedUnion = std::aligned_union_t<1, T, Ts...>;
+  alignas(alignof(AlignedUnion)) char buffer[sizeof(AlignedUnion)];
+};
+
+} // end namespace llvm
+
+#endif // LLVM_SUPPORT_ALIGNOF_H

diff  --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 53d65e1dba4c6..c0f7c10aefb45 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Config/abi-breaking.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -677,8 +678,8 @@ template <class T> class LLVM_NODISCARD Expected {
   }
 
   union {
-    std::aligned_union_t<1, storage_type> TStorage;
-    std::aligned_union_t<1, error_type> ErrorStorage;
+    AlignedCharArrayUnion<storage_type> TStorage;
+    AlignedCharArrayUnion<error_type> ErrorStorage;
   };
   bool HasError : 1;
 #if LLVM_ENABLE_ABI_BREAKING_CHECKS

diff  --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h
index d97b549871ac0..b654c9c9c43be 100644
--- a/llvm/include/llvm/Support/ErrorOr.h
+++ b/llvm/include/llvm/Support/ErrorOr.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_SUPPORT_ERROROR_H
 #define LLVM_SUPPORT_ERROROR_H
 
+#include "llvm/Support/AlignOf.h"
 #include <cassert>
 #include <system_error>
 #include <type_traits>
@@ -252,8 +253,8 @@ class ErrorOr {
   }
 
   union {
-    std::aligned_union_t<1, storage_type> TStorage;
-    std::aligned_union_t<1, std::error_code> ErrorStorage;
+    AlignedCharArrayUnion<storage_type> TStorage;
+    AlignedCharArrayUnion<std::error_code> ErrorStorage;
   };
   bool HasError : 1;
 };

diff  --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h
index 13af97032a046..c753cee60ec12 100644
--- a/llvm/include/llvm/Support/JSON.h
+++ b/llvm/include/llvm/Support/JSON.h
@@ -479,8 +479,8 @@ class Value {
   };
   // All members mutable, see moveFrom().
   mutable ValueType Type;
-  mutable std::aligned_union_t<1, bool, double, int64_t, llvm::StringRef,
-                               std::string, json::Array, json::Object>
+  mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, llvm::StringRef,
+                                      std::string, json::Array, json::Object>
       Union;
   friend bool operator==(const Value &, const Value &);
 };

diff  --git a/llvm/include/llvm/Support/TrailingObjects.h b/llvm/include/llvm/Support/TrailingObjects.h
index 54111fbeba50f..0d9c4503aa9be 100644
--- a/llvm/include/llvm/Support/TrailingObjects.h
+++ b/llvm/include/llvm/Support/TrailingObjects.h
@@ -46,6 +46,7 @@
 #ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
 #define LLVM_SUPPORT_TRAILINGOBJECTS_H
 
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Alignment.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MathExtras.h"

diff  --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 92fc4cff7e452..bdd27dd99e8a4 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -15,6 +15,7 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Regex.h"
@@ -1312,7 +1313,7 @@ struct MappingNormalization {
   TNorm* operator->() { return BufPtr; }
 
 private:
-  using Storage = std::aligned_union_t<1, TNorm>;
+  using Storage = AlignedCharArrayUnion<TNorm>;
 
   Storage       Buffer;
   IO           &io;
@@ -1349,7 +1350,7 @@ struct MappingNormalizationHeap {
   TNorm* operator->() { return BufPtr; }
 
 private:
-  using Storage = std::aligned_union_t<1, TNorm>;
+  using Storage = AlignedCharArrayUnion<TNorm>;
 
   Storage       Buffer;
   IO           &io;

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 4e560a73676d0..74ba23d343a0e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -26,6 +26,7 @@
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/Value.h"
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/KnownBits.h"
 #include "llvm/Transforms/InstCombine/InstCombiner.h"
@@ -119,7 +120,7 @@ namespace {
     // is overkill of this end.
     short IntVal = 0;
 
-    std::aligned_union_t<1, APFloat> FpValBuf;
+    AlignedCharArrayUnion<APFloat> FpValBuf;
   };
 
   /// FAddend is used to represent floating-point addend. An addend is

diff  --git a/llvm/unittests/Support/AlignOfTest.cpp b/llvm/unittests/Support/AlignOfTest.cpp
new file mode 100644
index 0000000000000..d8cabde6c727a
--- /dev/null
+++ b/llvm/unittests/Support/AlignOfTest.cpp
@@ -0,0 +1,237 @@
+//=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifdef _MSC_VER
+// Disable warnings about alignment-based structure padding.
+// This must be above the includes to suppress warnings in included templates.
+#pragma warning(disable:4324)
+#endif
+
+#include "llvm/Support/AlignOf.h"
+#include "llvm/Support/Compiler.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+// Disable warnings about questionable type definitions.
+// We're testing that even questionable types work with the alignment utilities.
+#ifdef _MSC_VER
+#pragma warning(disable:4584)
+#endif
+
+// Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
+// due to ambiguity warning.
+#ifdef __clang__
+#pragma clang diagnostic ignored "-Wunknown-pragmas"
+#pragma clang diagnostic ignored "-Winaccessible-base"
+#elif ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
+// Pragma based warning suppression was introduced in GGC 4.2.  Additionally
+// this warning is "enabled by default".  The warning still appears if -Wall is
+// suppressed.  Apparently GCC suppresses it when -w is specifed, which is odd.
+#pragma GCC diagnostic warning "-w"
+#endif
+
+// Define some fixed alignment types to use in these tests.
+struct alignas(1) A1 {};
+struct alignas(2) A2 {};
+struct alignas(4) A4 {};
+struct alignas(8) A8 {};
+
+struct S1 {};
+struct S2 { char a; };
+struct S3 { int x; };
+struct S4 { double y; };
+struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
+struct S6 { double f(); };
+struct D1 : S1 {};
+struct D2 : S6 { float g(); };
+struct D3 : S2 {};
+struct D4 : S2 { int x; };
+struct D5 : S3 { char c; };
+struct D6 : S2, S3 {};
+struct D7 : S1, S3 {};
+struct D8 : S1, D4, D5 { double x[2]; };
+struct D9 : S1, D1 { S1 s1; };
+struct V1 { virtual ~V1(); };
+struct V2 { int x; virtual ~V2(); };
+struct V3 : V1 {
+  ~V3() override;
+};
+struct V4 : virtual V2 { int y;
+  ~V4() override;
+};
+struct V5 : V4, V3 { double z;
+  ~V5() override;
+};
+struct V6 : S1 { virtual ~V6(); };
+struct V7 : virtual V2, virtual V6 {
+  ~V7() override;
+};
+struct V8 : V5, virtual V6, V7 { double zz;
+  ~V8() override;
+};
+
+double S6::f() { return 0.0; }
+float D2::g() { return 0.0f; }
+V1::~V1() {}
+V2::~V2() {}
+V3::~V3() {}
+V4::~V4() {}
+V5::~V5() {}
+V6::~V6() {}
+V7::~V7() {}
+V8::~V8() {}
+
+template <typename M> struct T { M m; };
+
+TEST(AlignOfTest, BasicAlignedArray) {
+  EXPECT_LE(1u, alignof(AlignedCharArrayUnion<A1>));
+  EXPECT_LE(2u, alignof(AlignedCharArrayUnion<A2>));
+  EXPECT_LE(4u, alignof(AlignedCharArrayUnion<A4>));
+  EXPECT_LE(8u, alignof(AlignedCharArrayUnion<A8>));
+
+  EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
+  EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
+  EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
+  EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
+
+  EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1>)));
+  EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1, A2>)));
+  EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1, A2, A4>)));
+  EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1, A2, A4, A8>)));
+
+  EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
+  EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
+  EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
+  EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
+
+  EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1[1]>)));
+  EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1[2], A2[1]>)));
+  EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1[42], A2[55], A4[13]>)));
+  EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1[2], A2[1], A4, A8>)));
+
+  EXPECT_EQ(1u,  sizeof(AlignedCharArrayUnion<A1[1]>));
+  EXPECT_EQ(2u,  sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
+  EXPECT_EQ(4u,  sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
+  EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
+                                              A4[3], A8>));
+
+  // For other tests we simply assert that the alignment of the union mathes
+  // that of the fundamental type and hope that we have any weird type
+  // productions that would trigger bugs.
+  EXPECT_EQ(alignof(T<char>), alignof(AlignedCharArrayUnion<char>));
+  EXPECT_EQ(alignof(T<short>), alignof(AlignedCharArrayUnion<short>));
+  EXPECT_EQ(alignof(T<int>), alignof(AlignedCharArrayUnion<int>));
+  EXPECT_EQ(alignof(T<long>), alignof(AlignedCharArrayUnion<long>));
+  EXPECT_EQ(alignof(T<long long>), alignof(AlignedCharArrayUnion<long long>));
+  EXPECT_EQ(alignof(T<float>), alignof(AlignedCharArrayUnion<float>));
+  EXPECT_EQ(alignof(T<double>), alignof(AlignedCharArrayUnion<double>));
+  EXPECT_EQ(alignof(T<long double>),
+            alignof(AlignedCharArrayUnion<long double>));
+  EXPECT_EQ(alignof(T<void *>), alignof(AlignedCharArrayUnion<void *>));
+  EXPECT_EQ(alignof(T<int *>), alignof(AlignedCharArrayUnion<int *>));
+  EXPECT_EQ(alignof(T<double (*)(double)>),
+            alignof(AlignedCharArrayUnion<double (*)(double)>));
+  EXPECT_EQ(alignof(T<double (S6::*)()>),
+            alignof(AlignedCharArrayUnion<double (S6::*)()>));
+  EXPECT_EQ(alignof(S1), alignof(AlignedCharArrayUnion<S1>));
+  EXPECT_EQ(alignof(S2), alignof(AlignedCharArrayUnion<S2>));
+  EXPECT_EQ(alignof(S3), alignof(AlignedCharArrayUnion<S3>));
+  EXPECT_EQ(alignof(S4), alignof(AlignedCharArrayUnion<S4>));
+  EXPECT_EQ(alignof(S5), alignof(AlignedCharArrayUnion<S5>));
+  EXPECT_EQ(alignof(S6), alignof(AlignedCharArrayUnion<S6>));
+  EXPECT_EQ(alignof(D1), alignof(AlignedCharArrayUnion<D1>));
+  EXPECT_EQ(alignof(D2), alignof(AlignedCharArrayUnion<D2>));
+  EXPECT_EQ(alignof(D3), alignof(AlignedCharArrayUnion<D3>));
+  EXPECT_EQ(alignof(D4), alignof(AlignedCharArrayUnion<D4>));
+  EXPECT_EQ(alignof(D5), alignof(AlignedCharArrayUnion<D5>));
+  EXPECT_EQ(alignof(D6), alignof(AlignedCharArrayUnion<D6>));
+  EXPECT_EQ(alignof(D7), alignof(AlignedCharArrayUnion<D7>));
+  EXPECT_EQ(alignof(D8), alignof(AlignedCharArrayUnion<D8>));
+  EXPECT_EQ(alignof(D9), alignof(AlignedCharArrayUnion<D9>));
+  EXPECT_EQ(alignof(V1), alignof(AlignedCharArrayUnion<V1>));
+  EXPECT_EQ(alignof(V2), alignof(AlignedCharArrayUnion<V2>));
+  EXPECT_EQ(alignof(V3), alignof(AlignedCharArrayUnion<V3>));
+  EXPECT_EQ(alignof(V4), alignof(AlignedCharArrayUnion<V4>));
+  EXPECT_EQ(alignof(V5), alignof(AlignedCharArrayUnion<V5>));
+  EXPECT_EQ(alignof(V6), alignof(AlignedCharArrayUnion<V6>));
+  EXPECT_EQ(alignof(V7), alignof(AlignedCharArrayUnion<V7>));
+
+  // Some versions of MSVC get this wrong somewhat disturbingly. The failure
+  // appears to be benign: alignof(V8) produces a preposterous value: 12
+#ifndef _MSC_VER
+  EXPECT_EQ(alignof(V8), alignof(AlignedCharArrayUnion<V8>));
+#endif
+
+  EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
+  EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
+  EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
+  EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
+  EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
+  EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
+  EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
+  EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
+  EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
+  EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
+  EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
+  EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
+  EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
+  EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
+  EXPECT_EQ(sizeof(long long),
+            sizeof(AlignedCharArrayUnion<long long>));
+  EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
+  EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
+  EXPECT_EQ(sizeof(long double),
+            sizeof(AlignedCharArrayUnion<long double>));
+  EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
+  EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
+  EXPECT_EQ(sizeof(double (*)(double)),
+            sizeof(AlignedCharArrayUnion<double (*)(double)>));
+  EXPECT_EQ(sizeof(double (S6::*)()),
+            sizeof(AlignedCharArrayUnion<double (S6::*)()>));
+  EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
+  EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
+  EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
+  EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
+  EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
+  EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
+  EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
+  EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
+  EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
+  EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
+  EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
+  EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
+  EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
+  EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
+  EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
+  EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
+  EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
+  EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
+  EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
+  EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
+  EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
+  EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
+  EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
+  EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
+  EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
+  EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
+  EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
+  EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
+  EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
+  EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
+  EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
+  EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
+
+  // Some versions of MSVC also get this wrong. The failure again appears to be
+  // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
+#ifndef _MSC_VER
+  EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
+#endif
+}
+} // end anonymous namespace

diff  --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt
index 44fff481d1b61..72d4987ecf113 100644
--- a/llvm/unittests/Support/CMakeLists.txt
+++ b/llvm/unittests/Support/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_unittest(SupportTests
   AlignmentTest.cpp
+  AlignOfTest.cpp
   AllocatorTest.cpp
   AnnotationsTest.cpp
   ARMAttributeParser.cpp


        


More information about the llvm-branch-commits mailing list