mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 18:54:02 +01:00
Fix up erroneous alignas usage while making this portable to GCC 4.7
Review by Chandler Carruth. llvm-svn: 163944
This commit is contained in:
parent
be0144b3f7
commit
c05c0c5f06
@ -68,10 +68,7 @@ inline unsigned alignOf() { return AlignOf<T>::Alignment; }
|
|||||||
/// integer literal can be used to specify an alignment constraint. Once built
|
/// integer literal can be used to specify an alignment constraint. Once built
|
||||||
/// up here, we can then begin to indirect between these using normal C++
|
/// up here, we can then begin to indirect between these using normal C++
|
||||||
/// template parameters.
|
/// template parameters.
|
||||||
template <size_t Alignment> struct AlignedCharArrayImpl {};
|
template <size_t Alignment> struct AlignedCharArrayImpl;
|
||||||
template <> struct AlignedCharArrayImpl<0> {
|
|
||||||
typedef char type;
|
|
||||||
};
|
|
||||||
|
|
||||||
// MSVC requires special handling here.
|
// MSVC requires special handling here.
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
@ -79,12 +76,12 @@ template <> struct AlignedCharArrayImpl<0> {
|
|||||||
#if __has_feature(cxx_alignas)
|
#if __has_feature(cxx_alignas)
|
||||||
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
||||||
template <> struct AlignedCharArrayImpl<x> { \
|
template <> struct AlignedCharArrayImpl<x> { \
|
||||||
typedef char alignas(x) type; \
|
char alignas(x) aligned; \
|
||||||
}
|
}
|
||||||
#elif defined(__clang__) || defined(__GNUC__)
|
#elif defined(__GNUC__)
|
||||||
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
||||||
template <> struct AlignedCharArrayImpl<x> { \
|
template <> struct AlignedCharArrayImpl<x> { \
|
||||||
typedef char type __attribute__((aligned(x))); \
|
char aligned __attribute__((aligned(x))); \
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
# error No supported align as directive.
|
# error No supported align as directive.
|
||||||
@ -112,14 +109,14 @@ LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8192);
|
|||||||
// alignments because __declspec(align(...)) doesn't actually work when it is
|
// alignments because __declspec(align(...)) doesn't actually work when it is
|
||||||
// a member of a by-value function argument in MSVC, even if the alignment
|
// a member of a by-value function argument in MSVC, even if the alignment
|
||||||
// request is something reasonably like 8-byte or 16-byte.
|
// request is something reasonably like 8-byte or 16-byte.
|
||||||
template <> struct AlignedCharArrayImpl<1> { typedef char type; };
|
template <> struct AlignedCharArrayImpl<1> { char aligned; };
|
||||||
template <> struct AlignedCharArrayImpl<2> { typedef short type; };
|
template <> struct AlignedCharArrayImpl<2> { short aligned; };
|
||||||
template <> struct AlignedCharArrayImpl<4> { typedef int type; };
|
template <> struct AlignedCharArrayImpl<4> { int aligned; };
|
||||||
template <> struct AlignedCharArrayImpl<8> { typedef double type; };
|
template <> struct AlignedCharArrayImpl<8> { double aligned; };
|
||||||
|
|
||||||
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
||||||
template <> struct AlignedCharArrayImpl<x> { \
|
template <> struct AlignedCharArrayImpl<x> { \
|
||||||
typedef __declspec(align(x)) char type; \
|
__declspec(align(x)) char aligned; \
|
||||||
}
|
}
|
||||||
LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
|
LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
|
||||||
LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
|
LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
|
||||||
@ -162,17 +159,11 @@ public:
|
|||||||
/// constrain the layout of this character array.
|
/// constrain the layout of this character array.
|
||||||
char buffer[sizeof(SizerImpl)];
|
char buffer[sizeof(SizerImpl)];
|
||||||
|
|
||||||
// Sadly, Clang and GCC both fail to align a character array properly even
|
private:
|
||||||
// with an explicit alignment attribute. To work around this, we union
|
// Tests seem to indicate that both Clang and GCC will properly register the
|
||||||
// the character array that will actually be used with a struct that contains
|
// alignment of a struct containing an aligned member, and this alignment
|
||||||
// a single aligned character member. Tests seem to indicate that both Clang
|
// should carry over to the character array in the union.
|
||||||
// and GCC will properly register the alignment of a struct containing an
|
llvm::AlignedCharArrayImpl<AlignOf<AlignerImpl>::Alignment> nonce_member;
|
||||||
// aligned member, and this alignment should carry over to the character
|
|
||||||
// array in the union.
|
|
||||||
struct {
|
|
||||||
typename llvm::AlignedCharArrayImpl<AlignOf<AlignerImpl>::Alignment>::type
|
|
||||||
nonce_inner_member;
|
|
||||||
} nonce_member;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace llvm
|
} // end namespace llvm
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//===- llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ----===//
|
//=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ----===//
|
||||||
//
|
//
|
||||||
// The LLVM Compiler Infrastructure
|
// The LLVM Compiler Infrastructure
|
||||||
//
|
//
|
||||||
@ -23,31 +23,25 @@ namespace {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Define some fixed alignment types to use in these tests.
|
// Define some fixed alignment types to use in these tests.
|
||||||
#if __cplusplus == 201103L || __has_feature(cxx_alignas)
|
#if __has_feature(cxx_alignas)
|
||||||
typedef char alignas(1) A1;
|
struct alignas(1) A1 { };
|
||||||
typedef char alignas(2) A2;
|
struct alignas(2) A2 { };
|
||||||
typedef char alignas(4) A4;
|
struct alignas(4) A4 { };
|
||||||
typedef char alignas(8) A8;
|
struct alignas(8) A8 { };
|
||||||
#elif defined(__clang__) || defined(__GNUC__)
|
#elif defined(__GNUC__)
|
||||||
typedef char A1 __attribute__((aligned(1)));
|
struct A1 { } __attribute__((aligned(1)));
|
||||||
typedef char A2 __attribute__((aligned(2)));
|
struct A2 { } __attribute__((aligned(2)));
|
||||||
typedef char A4 __attribute__((aligned(4)));
|
struct A4 { } __attribute__((aligned(4)));
|
||||||
typedef char A8 __attribute__((aligned(8)));
|
struct A8 { } __attribute__((aligned(8)));
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
typedef __declspec(align(1)) char A1;
|
__declspec(align(1)) struct A1 { };
|
||||||
typedef __declspec(align(2)) char A2;
|
__declspec(align(2)) struct A2 { };
|
||||||
typedef __declspec(align(4)) char A4;
|
__declspec(align(4)) struct A4 { };
|
||||||
typedef __declspec(align(8)) char A8;
|
__declspec(align(8)) struct A8 { };
|
||||||
#else
|
#else
|
||||||
# error No supported align as directive.
|
# error No supported align as directive.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Wrap the forced aligned types in structs to hack around compiler bugs.
|
|
||||||
struct SA1 { A1 a; };
|
|
||||||
struct SA2 { A2 a; };
|
|
||||||
struct SA4 { A4 a; };
|
|
||||||
struct SA8 { A8 a; };
|
|
||||||
|
|
||||||
struct S1 {};
|
struct S1 {};
|
||||||
struct S2 { char a; };
|
struct S2 { char a; };
|
||||||
struct S3 { int x; };
|
struct S3 { int x; };
|
||||||
@ -90,11 +84,7 @@ char LLVM_ATTRIBUTE_UNUSED test_arr2
|
|||||||
[AlignOf<A1>::Alignment > 0]
|
[AlignOf<A1>::Alignment > 0]
|
||||||
[AlignOf<A2>::Alignment > 0]
|
[AlignOf<A2>::Alignment > 0]
|
||||||
[AlignOf<A4>::Alignment > 0]
|
[AlignOf<A4>::Alignment > 0]
|
||||||
[AlignOf<A8>::Alignment > 0]
|
[AlignOf<A8>::Alignment > 0];
|
||||||
[AlignOf<SA1>::Alignment > 0]
|
|
||||||
[AlignOf<SA2>::Alignment > 0]
|
|
||||||
[AlignOf<SA4>::Alignment > 0]
|
|
||||||
[AlignOf<SA8>::Alignment > 0];
|
|
||||||
char LLVM_ATTRIBUTE_UNUSED test_arr3
|
char LLVM_ATTRIBUTE_UNUSED test_arr3
|
||||||
[AlignOf<S1>::Alignment > 0]
|
[AlignOf<S1>::Alignment > 0]
|
||||||
[AlignOf<S2>::Alignment > 0]
|
[AlignOf<S2>::Alignment > 0]
|
||||||
@ -123,20 +113,10 @@ char LLVM_ATTRIBUTE_UNUSED test_arr5
|
|||||||
[AlignOf<V8>::Alignment > 0];
|
[AlignOf<V8>::Alignment > 0];
|
||||||
|
|
||||||
TEST(AlignOfTest, BasicAlignmentInvariants) {
|
TEST(AlignOfTest, BasicAlignmentInvariants) {
|
||||||
// For a very strange reason, many compilers do not support this. Both Clang
|
EXPECT_LE(1u, alignOf<A1>());
|
||||||
// and GCC fail to align these properly.
|
EXPECT_LE(2u, alignOf<A2>());
|
||||||
EXPECT_EQ(1u, alignOf<A1>());
|
EXPECT_LE(4u, alignOf<A4>());
|
||||||
#if 0
|
EXPECT_LE(8u, alignOf<A8>());
|
||||||
EXPECT_EQ(2u, alignOf<A2>());
|
|
||||||
EXPECT_EQ(4u, alignOf<A4>());
|
|
||||||
EXPECT_EQ(8u, alignOf<A8>());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// But once wrapped in structs, the alignment is correctly managed.
|
|
||||||
EXPECT_LE(1u, alignOf<SA1>());
|
|
||||||
EXPECT_LE(2u, alignOf<SA2>());
|
|
||||||
EXPECT_LE(4u, alignOf<SA4>());
|
|
||||||
EXPECT_LE(8u, alignOf<SA8>());
|
|
||||||
|
|
||||||
EXPECT_EQ(1u, alignOf<char>());
|
EXPECT_EQ(1u, alignOf<char>());
|
||||||
EXPECT_LE(alignOf<char>(), alignOf<short>());
|
EXPECT_LE(alignOf<char>(), alignOf<short>());
|
||||||
@ -174,42 +154,38 @@ TEST(AlignOfTest, BasicAlignmentInvariants) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(AlignOfTest, BasicAlignedArray) {
|
TEST(AlignOfTest, BasicAlignedArray) {
|
||||||
// Note: this code exclusively uses the struct-wrapped arbitrarily aligned
|
EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<A1> >());
|
||||||
// types because of the bugs mentioned above where GCC and Clang both
|
EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<A2> >());
|
||||||
// disregard the arbitrary alignment specifier until the type is used to
|
EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<A4> >());
|
||||||
// declare a member of a struct.
|
EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<A8> >());
|
||||||
EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<SA1> >());
|
|
||||||
EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<SA2> >());
|
|
||||||
EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<SA4> >());
|
|
||||||
EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<SA8> >());
|
|
||||||
|
|
||||||
EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<SA1>));
|
EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
|
||||||
EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<SA2>));
|
EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
|
||||||
EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<SA4>));
|
EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
|
||||||
EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<SA8>));
|
EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
|
||||||
|
|
||||||
EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<SA1> >()));
|
EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1> >()));
|
||||||
EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<SA1, SA2> >()));
|
EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1, A2> >()));
|
||||||
EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<SA1, SA2, SA4> >()));
|
EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1, A2, A4> >()));
|
||||||
EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<SA1, SA2, SA4, SA8> >()));
|
EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1, A2, A4, A8> >()));
|
||||||
|
|
||||||
EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<SA1>));
|
EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
|
||||||
EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<SA1, SA2>));
|
EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
|
||||||
EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<SA1, SA2, SA4>));
|
EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
|
||||||
EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<SA1, SA2, SA4, SA8>));
|
EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
|
||||||
|
|
||||||
EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<SA1[1]> >()));
|
EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1[1]> >()));
|
||||||
EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<SA1[2], SA2[1]> >()));
|
EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1]> >()));
|
||||||
EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<SA1[42], SA2[55],
|
EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1[42], A2[55],
|
||||||
SA4[13]> >()));
|
A4[13]> >()));
|
||||||
EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<SA1[2], SA2[1],
|
EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1],
|
||||||
SA4, SA8> >()));
|
A4, A8> >()));
|
||||||
|
|
||||||
EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<SA1[1]>));
|
EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1[1]>));
|
||||||
EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<SA1[2], SA2[1]>));
|
EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
|
||||||
EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<SA1[3], SA2[2], SA4>));
|
EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
|
||||||
EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<SA1, SA2[3],
|
EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
|
||||||
SA4[3], SA8>));
|
A4[3], A8>));
|
||||||
|
|
||||||
// For other tests we simply assert that the alignment of the union mathes
|
// 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
|
// that of the fundamental type and hope that we have any weird type
|
||||||
|
Loading…
Reference in New Issue
Block a user