Skip to content

Commit d2ed9d6

Browse files
committed
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 microsoft/STL#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 4d8bf87. This reverts commit d10f986. This reverts commit 4b5dc15.
1 parent ce0c001 commit d2ed9d6

File tree

22 files changed

+315
-29
lines changed

22 files changed

+315
-29
lines changed

clang/include/clang/AST/APValue.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "llvm/ADT/FoldingSet.h"
2121
#include "llvm/ADT/PointerIntPair.h"
2222
#include "llvm/ADT/PointerUnion.h"
23-
#include <type_traits>
23+
#include "llvm/Support/AlignOf.h"
2424

2525
namespace clang {
2626
class AddrLabelExpr;
@@ -286,10 +286,9 @@ class APValue {
286286
struct MemberPointerData;
287287

288288
// We ensure elsewhere that Data is big enough for LV and MemberPointerData.
289-
typedef std::aligned_union_t<1, void *, APSInt, APFloat, ComplexAPSInt,
290-
ComplexAPFloat, Vec, Arr, StructData, UnionData,
291-
AddrLabelDiffData>
292-
DataType;
289+
typedef llvm::AlignedCharArrayUnion<void *, APSInt, APFloat, ComplexAPSInt,
290+
ComplexAPFloat, Vec, Arr, StructData,
291+
UnionData, AddrLabelDiffData> DataType;
293292
static const size_t DataSize = sizeof(DataType);
294293

295294
DataType Data;

clang/include/clang/AST/ASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/ADT/TinyPtrVector.h"
5858
#include "llvm/ADT/Triple.h"
5959
#include "llvm/ADT/iterator_range.h"
60+
#include "llvm/Support/AlignOf.h"
6061
#include "llvm/Support/Allocator.h"
6162
#include "llvm/Support/Casting.h"
6263
#include "llvm/Support/Compiler.h"

clang/include/clang/AST/ASTTypeTraits.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "clang/AST/TypeLoc.h"
2323
#include "clang/Basic/LLVM.h"
2424
#include "llvm/ADT/DenseMapInfo.h"
25-
#include <type_traits>
25+
#include "llvm/Support/AlignOf.h"
2626

2727
namespace llvm {
2828

@@ -456,8 +456,9 @@ class DynTypedNode {
456456
/// \c QualTypes, \c NestedNameSpecifierLocs, \c TypeLocs,
457457
/// \c TemplateArguments and \c TemplateArgumentLocs on the other hand do not
458458
/// have storage or unique pointers and thus need to be stored by value.
459-
std::aligned_union_t<1, const void *, TemplateArgument, TemplateArgumentLoc,
460-
NestedNameSpecifierLoc, QualType, TypeLoc>
459+
llvm::AlignedCharArrayUnion<const void *, TemplateArgument,
460+
TemplateArgumentLoc, NestedNameSpecifierLoc,
461+
QualType, TypeLoc>
461462
Storage;
462463
};
463464

clang/include/clang/AST/ParentMapContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class TraversalKindScope {
8989
/// Container for either a single DynTypedNode or for an ArrayRef to
9090
/// DynTypedNode. For use with ParentMap.
9191
class DynTypedNodeList {
92-
std::aligned_union_t<1, DynTypedNode, ArrayRef<DynTypedNode>> Storage;
92+
llvm::AlignedCharArrayUnion<DynTypedNode, ArrayRef<DynTypedNode>> Storage;
9393
bool IsSingleNode;
9494

9595
public:

clang/include/clang/Frontend/PrecompiledPreamble.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Lex/Preprocessor.h"
1818
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1919
#include "llvm/ADT/StringRef.h"
20+
#include "llvm/Support/AlignOf.h"
2021
#include "llvm/Support/MD5.h"
2122
#include <cstddef>
2223
#include <memory>
@@ -197,7 +198,7 @@ class PrecompiledPreamble {
197198

198199
private:
199200
Kind StorageKind = Kind::Empty;
200-
std::aligned_union_t<1, TempPCHFile, InMemoryPreamble> Storage = {};
201+
llvm::AlignedCharArrayUnion<TempPCHFile, InMemoryPreamble> Storage = {};
201202
};
202203

203204
/// Data used to determine if a file used in the preamble has been changed.

clang/include/clang/Sema/Overload.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/ADT/SmallPtrSet.h"
3232
#include "llvm/ADT/SmallVector.h"
3333
#include "llvm/ADT/StringRef.h"
34+
#include "llvm/Support/AlignOf.h"
3435
#include "llvm/Support/Allocator.h"
3536
#include "llvm/Support/Casting.h"
3637
#include "llvm/Support/ErrorHandling.h"

clang/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "llvm/ADT/STLExtras.h"
1313
#include "llvm/ADT/ScopeExit.h"
14+
#include "llvm/Support/AlignOf.h"
1415
#include "llvm/Support/Errno.h"
1516
#include "llvm/Support/Error.h"
1617
#include "llvm/Support/Path.h"

clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/ScopeExit.h"
17+
#include "llvm/Support/AlignOf.h"
1718
#include "llvm/Support/Errno.h"
1819
#include "llvm/Support/Mutex.h"
1920
#include "llvm/Support/Path.h"

clang/lib/Lex/PPDirectives.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/ADT/STLExtras.h"
4040
#include "llvm/ADT/StringSwitch.h"
4141
#include "llvm/ADT/StringRef.h"
42+
#include "llvm/Support/AlignOf.h"
4243
#include "llvm/Support/ErrorHandling.h"
4344
#include "llvm/Support/Path.h"
4445
#include <algorithm>

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/DenseMapInfo.h"
1717
#include "llvm/ADT/EpochTracker.h"
18+
#include "llvm/Support/AlignOf.h"
1819
#include "llvm/Support/Compiler.h"
1920
#include "llvm/Support/MathExtras.h"
2021
#include "llvm/Support/MemAlloc.h"
@@ -900,7 +901,7 @@ class SmallDenseMap
900901

901902
/// A "union" of an inline bucket array and the struct representing
902903
/// a large bucket. This union will be discriminated by the 'Small' bit.
903-
std::aligned_union_t<1, BucketT[InlineBuckets], LargeRep> storage;
904+
AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
904905

905906
public:
906907
explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
@@ -1040,7 +1041,7 @@ class SmallDenseMap
10401041

10411042
if (Small) {
10421043
// First move the inline buckets into a temporary storage.
1043-
std::aligned_union_t<1, BucketT[InlineBuckets]> TmpStorage;
1044+
AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
10441045
BucketT *TmpBegin = reinterpret_cast<BucketT *>(&TmpStorage);
10451046
BucketT *TmpEnd = TmpBegin;
10461047

llvm/include/llvm/ADT/IntervalMap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@
101101
#include "llvm/ADT/PointerIntPair.h"
102102
#include "llvm/ADT/SmallVector.h"
103103
#include "llvm/ADT/bit.h"
104+
#include "llvm/Support/AlignOf.h"
104105
#include "llvm/Support/Allocator.h"
105106
#include "llvm/Support/RecyclingAllocator.h"
106107
#include <algorithm>
107108
#include <cassert>
108109
#include <cstdint>
109110
#include <iterator>
110111
#include <new>
111-
#include <type_traits>
112112
#include <utility>
113113

114114
namespace llvm {
@@ -963,7 +963,7 @@ class IntervalMap {
963963

964964
private:
965965
// The root data is either a RootLeaf or a RootBranchData instance.
966-
std::aligned_union_t<1, RootLeaf, RootBranchData> data;
966+
AlignedCharArrayUnion<RootLeaf, RootBranchData> data;
967967

968968
// Tree height.
969969
// 0: Leaves in root.

llvm/include/llvm/CodeGen/DIE.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/iterator_range.h"
2323
#include "llvm/BinaryFormat/Dwarf.h"
2424
#include "llvm/CodeGen/DwarfStringPoolEntry.h"
25+
#include "llvm/Support/AlignOf.h"
2526
#include "llvm/Support/Allocator.h"
2627
#include <cassert>
2728
#include <cstddef>
@@ -367,9 +368,9 @@ class DIEValue {
367368
///
368369
/// All values that aren't standard layout (or are larger than 8 bytes)
369370
/// should be stored by reference instead of by value.
370-
using ValTy = std::aligned_union_t<1, DIEInteger, DIEString, DIEExpr,
371-
DIELabel, DIEDelta *, DIEEntry, DIEBlock *,
372-
DIELoc *, DIELocList, DIEBaseTypeRef *>;
371+
using ValTy = AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
372+
DIEDelta *, DIEEntry, DIEBlock *,
373+
DIELoc *, DIELocList, DIEBaseTypeRef *>;
373374

374375
static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
375376
sizeof(ValTy) <= sizeof(void *),

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/IR/Instructions.h"
3939
#include "llvm/IR/Metadata.h"
4040
#include "llvm/IR/Operator.h"
41+
#include "llvm/Support/AlignOf.h"
4142
#include "llvm/Support/AtomicOrdering.h"
4243
#include "llvm/Support/Casting.h"
4344
#include "llvm/Support/ErrorHandling.h"
@@ -2634,9 +2635,10 @@ template <> struct GraphTraits<SDNode*> {
26342635
///
26352636
/// This needs to be a union because the largest node differs on 32 bit systems
26362637
/// with 4 and 8 byte pointer alignment, respectively.
2637-
using LargestSDNode =
2638-
std::aligned_union_t<1, AtomicSDNode, TargetIndexSDNode, BlockAddressSDNode,
2639-
GlobalAddressSDNode, PseudoProbeSDNode>;
2638+
using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
2639+
BlockAddressSDNode,
2640+
GlobalAddressSDNode,
2641+
PseudoProbeSDNode>;
26402642

26412643
/// The SDNode class with the greatest alignment requirement.
26422644
using MostAlignedSDNode = GlobalAddressSDNode;

llvm/include/llvm/Support/AlignOf.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the AlignedCharArrayUnion class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_SUPPORT_ALIGNOF_H
14+
#define LLVM_SUPPORT_ALIGNOF_H
15+
16+
#include <type_traits>
17+
18+
namespace llvm {
19+
20+
/// A suitably aligned and sized character array member which can hold elements
21+
/// of any type.
22+
///
23+
/// These types may be arrays, structs, or any other types. This exposes a
24+
/// `buffer` member which can be used as suitable storage for a placement new of
25+
/// any of these types.
26+
template <typename T, typename... Ts> struct AlignedCharArrayUnion {
27+
using AlignedUnion = std::aligned_union_t<1, T, Ts...>;
28+
alignas(alignof(AlignedUnion)) char buffer[sizeof(AlignedUnion)];
29+
};
30+
31+
} // end namespace llvm
32+
33+
#endif // LLVM_SUPPORT_ALIGNOF_H

llvm/include/llvm/Support/Error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/StringExtras.h"
2020
#include "llvm/ADT/Twine.h"
2121
#include "llvm/Config/abi-breaking.h"
22+
#include "llvm/Support/AlignOf.h"
2223
#include "llvm/Support/Compiler.h"
2324
#include "llvm/Support/Debug.h"
2425
#include "llvm/Support/ErrorHandling.h"
@@ -677,8 +678,8 @@ template <class T> class LLVM_NODISCARD Expected {
677678
}
678679

679680
union {
680-
std::aligned_union_t<1, storage_type> TStorage;
681-
std::aligned_union_t<1, error_type> ErrorStorage;
681+
AlignedCharArrayUnion<storage_type> TStorage;
682+
AlignedCharArrayUnion<error_type> ErrorStorage;
682683
};
683684
bool HasError : 1;
684685
#if LLVM_ENABLE_ABI_BREAKING_CHECKS

llvm/include/llvm/Support/ErrorOr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_SUPPORT_ERROROR_H
1616
#define LLVM_SUPPORT_ERROROR_H
1717

18+
#include "llvm/Support/AlignOf.h"
1819
#include <cassert>
1920
#include <system_error>
2021
#include <type_traits>
@@ -252,8 +253,8 @@ class ErrorOr {
252253
}
253254

254255
union {
255-
std::aligned_union_t<1, storage_type> TStorage;
256-
std::aligned_union_t<1, std::error_code> ErrorStorage;
256+
AlignedCharArrayUnion<storage_type> TStorage;
257+
AlignedCharArrayUnion<std::error_code> ErrorStorage;
257258
};
258259
bool HasError : 1;
259260
};

llvm/include/llvm/Support/JSON.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ class Value {
479479
};
480480
// All members mutable, see moveFrom().
481481
mutable ValueType Type;
482-
mutable std::aligned_union_t<1, bool, double, int64_t, llvm::StringRef,
483-
std::string, json::Array, json::Object>
482+
mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, llvm::StringRef,
483+
std::string, json::Array, json::Object>
484484
Union;
485485
friend bool operator==(const Value &, const Value &);
486486
};

llvm/include/llvm/Support/TrailingObjects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
4747
#define LLVM_SUPPORT_TRAILINGOBJECTS_H
4848

49+
#include "llvm/Support/AlignOf.h"
4950
#include "llvm/Support/Alignment.h"
5051
#include "llvm/Support/Compiler.h"
5152
#include "llvm/Support/MathExtras.h"

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/StringMap.h"
1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/ADT/Twine.h"
18+
#include "llvm/Support/AlignOf.h"
1819
#include "llvm/Support/Allocator.h"
1920
#include "llvm/Support/Endian.h"
2021
#include "llvm/Support/Regex.h"
@@ -1312,7 +1313,7 @@ struct MappingNormalization {
13121313
TNorm* operator->() { return BufPtr; }
13131314

13141315
private:
1315-
using Storage = std::aligned_union_t<1, TNorm>;
1316+
using Storage = AlignedCharArrayUnion<TNorm>;
13161317

13171318
Storage Buffer;
13181319
IO &io;
@@ -1349,7 +1350,7 @@ struct MappingNormalizationHeap {
13491350
TNorm* operator->() { return BufPtr; }
13501351

13511352
private:
1352-
using Storage = std::aligned_union_t<1, TNorm>;
1353+
using Storage = AlignedCharArrayUnion<TNorm>;
13531354

13541355
Storage Buffer;
13551356
IO &io;

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/IR/PatternMatch.h"
2727
#include "llvm/IR/Type.h"
2828
#include "llvm/IR/Value.h"
29+
#include "llvm/Support/AlignOf.h"
2930
#include "llvm/Support/Casting.h"
3031
#include "llvm/Support/KnownBits.h"
3132
#include "llvm/Transforms/InstCombine/InstCombiner.h"
@@ -119,7 +120,7 @@ namespace {
119120
// is overkill of this end.
120121
short IntVal = 0;
121122

122-
std::aligned_union_t<1, APFloat> FpValBuf;
123+
AlignedCharArrayUnion<APFloat> FpValBuf;
123124
};
124125

125126
/// FAddend is used to represent floating-point addend. An addend is

0 commit comments

Comments
 (0)