From: David Blaikie Date: Fri, 14 Sep 2012 22:26:11 +0000 (+0000) Subject: Fix up erroneous alignas usage while making this portable to GCC 4.7 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=48b6a79b2d367ea2e8cf014d8af9d573889d2f7f;p=oota-llvm.git Fix up erroneous alignas usage while making this portable to GCC 4.7 Review by Chandler Carruth. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@163944 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/AlignOf.h b/include/llvm/Support/AlignOf.h index 8c389afa80d..22c07d04fad 100644 --- a/include/llvm/Support/AlignOf.h +++ b/include/llvm/Support/AlignOf.h @@ -68,10 +68,7 @@ inline unsigned alignOf() { return AlignOf::Alignment; } /// 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++ /// template parameters. -template struct AlignedCharArrayImpl {}; -template <> struct AlignedCharArrayImpl<0> { - typedef char type; -}; +template struct AlignedCharArrayImpl; // MSVC requires special handling here. #ifndef _MSC_VER @@ -79,12 +76,12 @@ template <> struct AlignedCharArrayImpl<0> { #if __has_feature(cxx_alignas) #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \ template <> struct AlignedCharArrayImpl { \ - typedef char alignas(x) type; \ + char alignas(x) aligned; \ } -#elif defined(__clang__) || defined(__GNUC__) +#elif defined(__GNUC__) #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \ template <> struct AlignedCharArrayImpl { \ - typedef char type __attribute__((aligned(x))); \ + char aligned __attribute__((aligned(x))); \ } #else # 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 // a member of a by-value function argument in MSVC, even if the alignment // request is something reasonably like 8-byte or 16-byte. -template <> struct AlignedCharArrayImpl<1> { typedef char type; }; -template <> struct AlignedCharArrayImpl<2> { typedef short type; }; -template <> struct AlignedCharArrayImpl<4> { typedef int type; }; -template <> struct AlignedCharArrayImpl<8> { typedef double type; }; +template <> struct AlignedCharArrayImpl<1> { char aligned; }; +template <> struct AlignedCharArrayImpl<2> { short aligned; }; +template <> struct AlignedCharArrayImpl<4> { int aligned; }; +template <> struct AlignedCharArrayImpl<8> { double aligned; }; #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \ template <> struct AlignedCharArrayImpl { \ - typedef __declspec(align(x)) char type; \ + __declspec(align(x)) char aligned; \ } LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16); LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32); @@ -162,17 +159,11 @@ public: /// constrain the layout of this character array. char buffer[sizeof(SizerImpl)]; - // Sadly, Clang and GCC both fail to align a character array properly even - // with an explicit alignment attribute. To work around this, we union - // the character array that will actually be used with a struct that contains - // a single aligned character member. Tests seem to indicate that both Clang - // and GCC will properly register the alignment of a struct containing an - // aligned member, and this alignment should carry over to the character - // array in the union. - struct { - typename llvm::AlignedCharArrayImpl::Alignment>::type - nonce_inner_member; - } nonce_member; +private: + // Tests seem to indicate that both Clang and GCC will properly register the + // alignment of a struct containing an aligned member, and this alignment + // should carry over to the character array in the union. + llvm::AlignedCharArrayImpl::Alignment> nonce_member; }; } // end namespace llvm diff --git a/unittests/Support/AlignOfTest.cpp b/unittests/Support/AlignOfTest.cpp index 6f576681a3e..f2d11708a2b 100644 --- a/unittests/Support/AlignOfTest.cpp +++ b/unittests/Support/AlignOfTest.cpp @@ -1,4 +1,4 @@ -//===- llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ----===// +//=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ----===// // // The LLVM Compiler Infrastructure // @@ -23,31 +23,25 @@ namespace { #endif // Define some fixed alignment types to use in these tests. -#if __cplusplus == 201103L || __has_feature(cxx_alignas) -typedef char alignas(1) A1; -typedef char alignas(2) A2; -typedef char alignas(4) A4; -typedef char alignas(8) A8; -#elif defined(__clang__) || defined(__GNUC__) -typedef char A1 __attribute__((aligned(1))); -typedef char A2 __attribute__((aligned(2))); -typedef char A4 __attribute__((aligned(4))); -typedef char A8 __attribute__((aligned(8))); +#if __has_feature(cxx_alignas) +struct alignas(1) A1 { }; +struct alignas(2) A2 { }; +struct alignas(4) A4 { }; +struct alignas(8) A8 { }; +#elif defined(__GNUC__) +struct A1 { } __attribute__((aligned(1))); +struct A2 { } __attribute__((aligned(2))); +struct A4 { } __attribute__((aligned(4))); +struct A8 { } __attribute__((aligned(8))); #elif defined(_MSC_VER) -typedef __declspec(align(1)) char A1; -typedef __declspec(align(2)) char A2; -typedef __declspec(align(4)) char A4; -typedef __declspec(align(8)) char A8; +__declspec(align(1)) struct A1 { }; +__declspec(align(2)) struct A2 { }; +__declspec(align(4)) struct A4 { }; +__declspec(align(8)) struct A8 { }; #else # error No supported align as directive. #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 S2 { char a; }; struct S3 { int x; }; @@ -90,11 +84,7 @@ char LLVM_ATTRIBUTE_UNUSED test_arr2 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] - [AlignOf::Alignment > 0] - [AlignOf::Alignment > 0] - [AlignOf::Alignment > 0] - [AlignOf::Alignment > 0] - [AlignOf::Alignment > 0]; + [AlignOf::Alignment > 0]; char LLVM_ATTRIBUTE_UNUSED test_arr3 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] @@ -123,20 +113,10 @@ char LLVM_ATTRIBUTE_UNUSED test_arr5 [AlignOf::Alignment > 0]; TEST(AlignOfTest, BasicAlignmentInvariants) { - // For a very strange reason, many compilers do not support this. Both Clang - // and GCC fail to align these properly. - EXPECT_EQ(1u, alignOf()); -#if 0 - EXPECT_EQ(2u, alignOf()); - EXPECT_EQ(4u, alignOf()); - EXPECT_EQ(8u, alignOf()); -#endif - - // But once wrapped in structs, the alignment is correctly managed. - EXPECT_LE(1u, alignOf()); - EXPECT_LE(2u, alignOf()); - EXPECT_LE(4u, alignOf()); - EXPECT_LE(8u, alignOf()); + EXPECT_LE(1u, alignOf()); + EXPECT_LE(2u, alignOf()); + EXPECT_LE(4u, alignOf()); + EXPECT_LE(8u, alignOf()); EXPECT_EQ(1u, alignOf()); EXPECT_LE(alignOf(), alignOf()); @@ -174,42 +154,38 @@ TEST(AlignOfTest, BasicAlignmentInvariants) { } TEST(AlignOfTest, BasicAlignedArray) { - // Note: this code exclusively uses the struct-wrapped arbitrarily aligned - // types because of the bugs mentioned above where GCC and Clang both - // disregard the arbitrary alignment specifier until the type is used to - // declare a member of a struct. - EXPECT_LE(1u, alignOf >()); - EXPECT_LE(2u, alignOf >()); - EXPECT_LE(4u, alignOf >()); - EXPECT_LE(8u, alignOf >()); + EXPECT_LE(1u, alignOf >()); + EXPECT_LE(2u, alignOf >()); + EXPECT_LE(4u, alignOf >()); + EXPECT_LE(8u, alignOf >()); - EXPECT_LE(1u, sizeof(AlignedCharArrayUnion)); - EXPECT_LE(2u, sizeof(AlignedCharArrayUnion)); - EXPECT_LE(4u, sizeof(AlignedCharArrayUnion)); - EXPECT_LE(8u, sizeof(AlignedCharArrayUnion)); + EXPECT_LE(1u, sizeof(AlignedCharArrayUnion)); + EXPECT_LE(2u, sizeof(AlignedCharArrayUnion)); + EXPECT_LE(4u, sizeof(AlignedCharArrayUnion)); + EXPECT_LE(8u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(1u, (alignOf >())); - EXPECT_EQ(2u, (alignOf >())); - EXPECT_EQ(4u, (alignOf >())); - EXPECT_EQ(8u, (alignOf >())); + EXPECT_EQ(1u, (alignOf >())); + EXPECT_EQ(2u, (alignOf >())); + EXPECT_EQ(4u, (alignOf >())); + EXPECT_EQ(8u, (alignOf >())); - EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(1u, (alignOf >())); - EXPECT_EQ(2u, (alignOf >())); - EXPECT_EQ(4u, (alignOf >())); - EXPECT_EQ(8u, (alignOf >())); + EXPECT_EQ(1u, (alignOf >())); + EXPECT_EQ(2u, (alignOf >())); + EXPECT_EQ(4u, (alignOf >())); + EXPECT_EQ(8u, (alignOf >())); - EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion)); - EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion)); + EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion)); // 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