[PATCH] Introduce __BIGGEST_ALIGNMENT__ macro

Reid Kleckner rnk at google.com
Sun Dec 14 22:41:02 PST 2014


Is there a compelling reason to use __BIGGEST_ALIGNMENT__ over max_align_t?

On Sat, Dec 13, 2014 at 9:51 AM, mats petersson <mats at planetcatfish.com>
wrote:
>
> This is my first attempt for a patch to Clang, so please accept my
> apologies for any misconduct with regards to formatting, styling,
> content, culture, etc - and I hope that gmail doesn't munge the
> patch...
>
> The idea for this came about when at work (ARM) we discussed some code
> that had accidentally aligned it's sub-allocated block to 4 bytes for
> a 64-bit type, and got a unaligned access trap. So "how do you know",
> and someone found __BIGGEST_ALIGNMENT__ in gcc - but not available for
> Clang. So I thought I could add it, to make it a little more
> consistent between compilers...
>
> --
> Mats
>
> Add a predefined macro reflecting the largest alignment required by
> the archiecture, in bytes. This is useful for allocator designs that
> need to know the system alignment requirements. It reflects the
> TargetInfo.SuitableAlignment, converted to bytes (using CharWidth).
> ---
>  lib/Frontend/InitPreprocessor.cpp |    4 ++++
>  test/Preprocessor/init.c          |   33 +++++++++++++++++++++++++++++++++
>  2 files changed, 37 insertions(+), 0 deletions(-)
>
> diff --git a/lib/Frontend/InitPreprocessor.cpp
> b/lib/Frontend/InitPreprocessor.cpp
> index f4241a9..896277a 100644
> --- a/lib/Frontend/InitPreprocessor.cpp
> +++ b/lib/Frontend/InitPreprocessor.cpp
> @@ -710,6 +710,10 @@ static void InitializePredefinedMacros(const
> TargetInfo &TI,
>    Builder.defineMacro("__POINTER_WIDTH__",
>                        Twine((int)TI.getPointerWidth(0)));
>
> +  // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
> +  Builder.defineMacro("__BIGGEST_ALIGNMENT__",
> +                      Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
> +
>    if (!LangOpts.CharIsSigned)
>      Builder.defineMacro("__CHAR_UNSIGNED__");
>
> diff --git a/test/Preprocessor/init.c b/test/Preprocessor/init.c
> index 4f32114..924aa5b 100644
> --- a/test/Preprocessor/init.c
> +++ b/test/Preprocessor/init.c
> @@ -241,6 +241,7 @@
>  // AARCH64:#define __ARM_ARCH 8
>  // AARCH64:#define __ARM_ARCH_ISA_A64 1
>  // AARCH64-NOT:#define __ARM_BIG_ENDIAN 1
> +// AARCH64:#define __BIGGEST_ALIGNMENT__ 8
>  // AARCH64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // AARCH64:#define __CHAR16_TYPE__ unsigned short
>  // AARCH64:#define __CHAR32_TYPE__ unsigned int
> @@ -431,6 +432,7 @@
>  // AARCH64-BE:#define __ARM_ARCH 8
>  // AARCH64-BE:#define __ARM_ARCH_ISA_A64 1
>  // AARCH64-BE:#define __ARM_BIG_ENDIAN 1
> +// AARCH64-BE:#define __BIGGEST_ALIGNMENT__ 8
>  // AARCH64-BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // AARCH64-BE:#define __CHAR16_TYPE__ unsigned short
>  // AARCH64-BE:#define __CHAR32_TYPE__ unsigned int
> @@ -621,6 +623,7 @@
>  // AARCH64-NETBSD:#define __ARM_ARCH 8
>  // AARCH64-NETBSD:#define __ARM_ARCH_ISA_A64 1
>  // AARCH64-NETBSD-NOT:#define __ARM_BIG_ENDIAN 1
> +// AARCH64-NETBSD:#define __BIGGEST_ALIGNMENT__ 8
>  // AARCH64-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // AARCH64-NETBSD:#define __CHAR16_TYPE__ unsigned short
>  // AARCH64-NETBSD:#define __CHAR32_TYPE__ unsigned int
> @@ -812,6 +815,7 @@
>  // AARCH64-FREEBSD:#define __ARM_ARCH 8
>  // AARCH64-FREEBSD:#define __ARM_ARCH_ISA_A64 1
>  // AARCH64-FREEBSD-NOT:#define __ARM_BIG_ENDIAN 1
> +// AARCH64-FREEBSD:#define __BIGGEST_ALIGNMENT__ 8
>  // AARCH64-FREEBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // AARCH64-FREEBSD:#define __CHAR16_TYPE__ unsigned short
>  // AARCH64-FREEBSD:#define __CHAR32_TYPE__ unsigned int
> @@ -1003,6 +1007,7 @@
>  // ARM:#define __ARMEL__ 1
>  // ARM:#define __ARM_ARCH_6J__ 1
>  // ARM-NOT:#define __ARM_BIG_ENDIAN 1
> +// ARM:#define __BIGGEST_ALIGNMENT__ 8
>  // ARM:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // ARM:#define __CHAR16_TYPE__ unsigned short
>  // ARM:#define __CHAR32_TYPE__ unsigned int
> @@ -1193,6 +1198,7 @@
>  // ARM-BE-NOT:#define __ARMEL__ 1
>  // ARM-BE:#define __ARM_ARCH_6J__ 1
>  // ARM-BE:#define __ARM_BIG_ENDIAN 1
> +// ARM-BE:#define __BIGGEST_ALIGNMENT__ 8
>  // ARM-BE:#define __BIG_ENDIAN__ 1
>  // ARM-BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // ARM-BE:#define __CHAR16_TYPE__ unsigned short
> @@ -1387,6 +1393,7 @@
>  // ARMEABISOFTFP:#define __ARM_EABI__ 1
>  // ARMEABISOFTFP:#define __ARM_PCS 1
>  // ARMEABISOFTFP-NOT:#define __ARM_PCS_VFP 1
> +// ARMEABISOFTFP:#define __BIGGEST_ALIGNMENT__ 8
>  // ARMEABISOFTFP:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // ARMEABISOFTFP:#define __CHAR16_TYPE__ unsigned short
>  // ARMEABISOFTFP:#define __CHAR32_TYPE__ unsigned int
> @@ -1582,6 +1589,7 @@
>  // ARMEABIHARDFP:#define __ARM_EABI__ 1
>  // ARMEABIHARDFP:#define __ARM_PCS 1
>  // ARMEABIHARDFP:#define __ARM_PCS_VFP 1
> +// ARMEABIHARDFP:#define __BIGGEST_ALIGNMENT__ 8
>  // ARMEABIHARDFP:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // ARMEABIHARDFP:#define __CHAR16_TYPE__ unsigned short
>  // ARMEABIHARDFP:#define __CHAR32_TYPE__ unsigned int
> @@ -1775,6 +1783,7 @@
>  // ARM-NETBSD:#define __ARM_DWARF_EH__ 1
>  // ARM-NETBSD:#define __ARM_EABI__ 1
>  // ARM-NETBSD-NOT:#define __ARM_BIG_ENDIAN 1
> +// ARM-NETBSD:#define __BIGGEST_ALIGNMENT__ 8
>  // ARM-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // ARM-NETBSD:#define __CHAR16_TYPE__ unsigned short
>  // ARM-NETBSD:#define __CHAR32_TYPE__ unsigned int
> @@ -2021,6 +2030,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-none-none <
> /dev/null | FileCheck -check-prefix I386 %s
>  //
>  // I386-NOT:#define _LP64
> +// I386:#define __BIGGEST_ALIGNMENT__ 16
>  // I386:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // I386:#define __CHAR16_TYPE__ unsigned short
>  // I386:#define __CHAR32_TYPE__ unsigned int
> @@ -2207,6 +2217,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-pc-linux-gnu
> -target-cpu pentium4 < /dev/null | FileCheck -check-prefix I386-LINUX
> %s
>  //
>  // I386-LINUX-NOT:#define _LP64
> +// I386-LINUX:#define __BIGGEST_ALIGNMENT__ 16
>  // I386-LINUX:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // I386-LINUX:#define __CHAR16_TYPE__ unsigned short
>  // I386-LINUX:#define __CHAR32_TYPE__ unsigned int
> @@ -2393,6 +2404,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-netbsd <
> /dev/null | FileCheck -check-prefix I386-NETBSD %s
>  //
>  // I386-NETBSD-NOT:#define _LP64
> +// I386-NETBSD:#define __BIGGEST_ALIGNMENT__ 16
>  // I386-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // I386-NETBSD:#define __CHAR16_TYPE__ unsigned short
>  // I386-NETBSD:#define __CHAR32_TYPE__ unsigned int
> @@ -2597,6 +2609,7 @@
>  // MIPS32BE:#define _MIPS_SZINT 32
>  // MIPS32BE:#define _MIPS_SZLONG 32
>  // MIPS32BE:#define _MIPS_SZPTR 32
> +// MIPS32BE:#define __BIGGEST_ALIGNMENT__ 8
>  // MIPS32BE:#define __BIG_ENDIAN__ 1
>  // MIPS32BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // MIPS32BE:#define __CHAR16_TYPE__ unsigned short
> @@ -2805,6 +2818,7 @@
>  // MIPS32EL:#define _MIPS_SZINT 32
>  // MIPS32EL:#define _MIPS_SZLONG 32
>  // MIPS32EL:#define _MIPS_SZPTR 32
> +// MIPS32EL:#define __BIGGEST_ALIGNMENT__ 8
>  // MIPS32EL:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // MIPS32EL:#define __CHAR16_TYPE__ unsigned short
>  // MIPS32EL:#define __CHAR32_TYPE__ unsigned int
> @@ -3010,6 +3024,7 @@
>  // MIPS64BE:#define _MIPS_SZINT 32
>  // MIPS64BE:#define _MIPS_SZLONG 64
>  // MIPS64BE:#define _MIPS_SZPTR 64
> +// MIPS64BE:#define __BIGGEST_ALIGNMENT__ 16
>  // MIPS64BE:#define __BIG_ENDIAN__ 1
>  // MIPS64BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // MIPS64BE:#define __CHAR16_TYPE__ unsigned short
> @@ -3218,6 +3233,7 @@
>  // MIPS64EL:#define _MIPS_SZINT 32
>  // MIPS64EL:#define _MIPS_SZLONG 64
>  // MIPS64EL:#define _MIPS_SZPTR 64
> +// MIPS64EL:#define __BIGGEST_ALIGNMENT__ 16
>  // MIPS64EL:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // MIPS64EL:#define __CHAR16_TYPE__ unsigned short
>  // MIPS64EL:#define __CHAR32_TYPE__ unsigned int
> @@ -3583,6 +3599,7 @@
>  //
>  // MSP430:#define MSP430 1
>  // MSP430-NOT:#define _LP64
> +// MSP430:#define __BIGGEST_ALIGNMENT__ 2
>  // MSP430:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // MSP430:#define __CHAR16_TYPE__ unsigned short
>  // MSP430:#define __CHAR32_TYPE__ unsigned int
> @@ -3766,6 +3783,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=nvptx-none-none <
> /dev/null | FileCheck -check-prefix NVPTX32 %s
>  //
>  // NVPTX32-NOT:#define _LP64
> +// NVPTX32:#define __BIGGEST_ALIGNMENT__ 8
>  // NVPTX32:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // NVPTX32:#define __CHAR16_TYPE__ unsigned short
>  // NVPTX32:#define __CHAR32_TYPE__ unsigned int
> @@ -3952,6 +3970,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=nvptx64-none-none <
> /dev/null | FileCheck -check-prefix NVPTX64 %s
>  //
>  // NVPTX64:#define _LP64 1
> +// NVPTX64:#define __BIGGEST_ALIGNMENT__ 8
>  // NVPTX64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // NVPTX64:#define __CHAR16_TYPE__ unsigned short
>  // NVPTX64:#define __CHAR32_TYPE__ unsigned int
> @@ -4143,6 +4162,7 @@
>  // PPC603E:#define _ARCH_PPCGR 1
>  // PPC603E:#define _BIG_ENDIAN 1
>  // PPC603E-NOT:#define _LP64
> +// PPC603E:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC603E:#define __BIG_ENDIAN__ 1
>  // PPC603E:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC603E:#define __CHAR16_TYPE__ unsigned short
> @@ -4340,6 +4360,7 @@
>  // PPC64:#define _ARCH_PWR7 1
>  // PPC64:#define _BIG_ENDIAN 1
>  // PPC64:#define _LP64 1
> +// PPC64:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC64:#define __BIG_ENDIAN__ 1
>  // PPC64:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC64:#define __CHAR16_TYPE__ unsigned short
> @@ -4542,6 +4563,7 @@
>  // PPC64LE:#define _CALL_ELF 2
>  // PPC64LE:#define _LITTLE_ENDIAN 1
>  // PPC64LE:#define _LP64 1
> +// PPC64LE:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC64LE:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // PPC64LE:#define __CHAR16_TYPE__ unsigned short
>  // PPC64LE:#define __CHAR32_TYPE__ unsigned int
> @@ -4923,6 +4945,7 @@
>  // PPC64-LINUX:#define _ARCH_PPC64 1
>  // PPC64-LINUX:#define _BIG_ENDIAN 1
>  // PPC64-LINUX:#define _LP64 1
> +// PPC64-LINUX:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC64-LINUX:#define __BIG_ENDIAN__ 1
>  // PPC64-LINUX:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC64-LINUX:#define __CHAR16_TYPE__ unsigned short
> @@ -5127,6 +5150,7 @@
>  // PPC:#define _ARCH_PPC 1
>  // PPC:#define _BIG_ENDIAN 1
>  // PPC-NOT:#define _LP64
> +// PPC:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC:#define __BIG_ENDIAN__ 1
>  // PPC:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC:#define __CHAR16_TYPE__ unsigned short
> @@ -5317,6 +5341,7 @@
>  // PPC-LINUX:#define _ARCH_PPC 1
>  // PPC-LINUX:#define _BIG_ENDIAN 1
>  // PPC-LINUX-NOT:#define _LP64
> +// PPC-LINUX:#define __BIGGEST_ALIGNMENT__ 8
>  // PPC-LINUX:#define __BIG_ENDIAN__ 1
>  // PPC-LINUX:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC-LINUX:#define __CHAR16_TYPE__ unsigned short
> @@ -5508,6 +5533,7 @@
>  //
>  // PPC-DARWIN:#define _ARCH_PPC 1
>  // PPC-DARWIN:#define _BIG_ENDIAN 1
> +// PPC-DARWIN:#define __BIGGEST_ALIGNMENT__ 16
>  // PPC-DARWIN:#define __BIG_ENDIAN__ 1
>  // PPC-DARWIN:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // PPC-DARWIN:#define __CHAR16_TYPE__ unsigned short
> @@ -5701,6 +5727,7 @@
>  //
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=s390x-none-none
> -fno-signed-char < /dev/null | FileCheck -check-prefix S390X %s
>  //
> +// S390X:#define __BIGGEST_ALIGNMENT__ 8
>  // S390X:#define __CHAR16_TYPE__ unsigned short
>  // S390X:#define __CHAR32_TYPE__ unsigned int
>  // S390X:#define __CHAR_BIT__ 8
> @@ -5882,6 +5909,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=sparc-none-none <
> /dev/null | FileCheck -check-prefix SPARC %s
>  //
>  // SPARC-NOT:#define _LP64
> +// SPARC:#define __BIGGEST_ALIGNMENT__ 8
>  // SPARC:#define __BIG_ENDIAN__ 1
>  // SPARC:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // SPARC:#define __CHAR16_TYPE__ unsigned short
> @@ -6069,6 +6097,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=tce-none-none <
> /dev/null | FileCheck -check-prefix TCE %s
>  //
>  // TCE-NOT:#define _LP64
> +// TCE:#define __BIGGEST_ALIGNMENT__ 4
>  // TCE:#define __BIG_ENDIAN__ 1
>  // TCE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
>  // TCE:#define __CHAR16_TYPE__ unsigned short
> @@ -6236,6 +6265,7 @@
>  //
>  // X86_64:#define _LP64 1
>  // X86_64-NOT:#define _LP32 1
> +// X86_64:#define __BIGGEST_ALIGNMENT__ 16
>  // X86_64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // X86_64:#define __CHAR16_TYPE__ unsigned short
>  // X86_64:#define __CHAR32_TYPE__ unsigned int
> @@ -6437,6 +6467,7 @@
>  //
>  // X32:#define _ILP32 1
>  // X32-NOT:#define _LP64 1
> +// X32:#define __BIGGEST_ALIGNMENT__ 16
>  // X32:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // X32:#define __CHAR16_TYPE__ unsigned short
>  // X32:#define __CHAR32_TYPE__ unsigned int
> @@ -6630,6 +6661,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=x86_64-pc-linux-gnu
> < /dev/null | FileCheck -check-prefix X86_64-LINUX %s
>  //
>  // X86_64-LINUX:#define _LP64 1
> +// X86_64-LINUX:#define __BIGGEST_ALIGNMENT__ 16
>  // X86_64-LINUX:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // X86_64-LINUX:#define __CHAR16_TYPE__ unsigned short
>  // X86_64-LINUX:#define __CHAR32_TYPE__ unsigned int
> @@ -6828,6 +6860,7 @@
>  // RUN: %clang_cc1 -E -dM -ffreestanding -triple=x86_64-netbsd <
> /dev/null | FileCheck -check-prefix X86_64-NETBSD %s
>  //
>  // X86_64-NETBSD:#define _LP64 1
> +// X86_64-NETBSD:#define __BIGGEST_ALIGNMENT__ 16
>  // X86_64-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
>  // X86_64-NETBSD:#define __CHAR16_TYPE__ unsigned short
>  // X86_64-NETBSD:#define __CHAR32_TYPE__ unsigned int
> --
> 1.7.7.6
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141214/6755fd7e/attachment.html>


More information about the cfe-commits mailing list