[llvm-commits] [llvm] r162549 - /llvm/trunk/include/llvm/Support/AlignOf.h

Chandler Carruth chandlerc at gmail.com
Fri Aug 24 02:53:44 PDT 2012

Author: chandlerc
Date: Fri Aug 24 04:53:43 2012
New Revision: 162549

URL: http://llvm.org/viewvc/llvm-project?rev=162549&view=rev
Try to appease MSVC even more elaborately in the alignment hacking space.

MSVC doesn't support passing by-value parameters with alignment of
16-bytes or higher apparantly. What is deeply confusing is that it seems
to *sometimes* (but not always) apply this to any type whose alignment
is set using __declspec(align(...)). This caused lots of errors when we switch
SmallVector over to use the automatically aligned character array
utilities as they used __declspec(align(...)) heavily.

As a pretty horrible but effective work-around, we instead cherry pick
the smallest alignment sizes with specific types that happen to have the
correct alignment, and then fall back to the attribute solution past
them. This should resolve the MSVC build errors folks have been hitting.
Sorry for that. In good news, it will do this without introducing other
UB I hope. =]

Thanks to Timur Iskhodzhanov for helping me test this!


Modified: llvm/trunk/include/llvm/Support/AlignOf.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/AlignOf.h?rev=162549&r1=162548&r2=162549&view=diff
--- llvm/trunk/include/llvm/Support/AlignOf.h (original)
+++ llvm/trunk/include/llvm/Support/AlignOf.h Fri Aug 24 04:53:43 2012
@@ -72,6 +72,10 @@
 template <> struct AlignedCharArrayImpl<0> {
   typedef char type;
+// MSVC requires special handling here.
+#ifndef _MSC_VER
 #if __has_feature(cxx_alignas)
   template <> struct AlignedCharArrayImpl<x> { \
@@ -82,11 +86,6 @@
   template <> struct AlignedCharArrayImpl<x> { \
     typedef char type __attribute__((aligned(x))); \
-#elif defined(_MSC_VER)
-  template <> struct AlignedCharArrayImpl<x> { \
-    typedef __declspec(align(x)) char type; \
-  }
 # error No supported align as directive.
@@ -104,9 +103,38 @@
+#else // _MSC_VER
+// We provide special variations of this template for the most common
+// 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<x> { \
+    typedef __declspec(align(x)) char type; \
+  }
 // Any larger and MSVC complains.
+#endif // _MSC_VER
 /// \brief This union template exposes a suitably aligned and sized character
 /// array member which can hold elements of any of up to four types.

More information about the llvm-commits mailing list