From 63012d9a57edc950c5f30242d1e19318b5708060 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Mon, 19 Sep 2016 22:51:56 +0100 Subject: [PATCH] Make max_align_t respect _Float128. The _FloatN, _FloatNx, _DecimalN and _DecimalNx types are specified in such a way that they are basic types, meaning that max_align_t must be at least as aligned as those types. On 32-bit x86, max_align_t is currently 8-byte aligned, but _Decimal128 and _Float128 are 16-byte aligned, so the alignment of max_align_t needs to increase to meet the standard requirements for these types. This patch implements such an increase. Because max_align_t needs to be usable for C++ as well as for C, can't actually refer to _Float128, but needs to use __float128 (or some other notation for the type) instead. And since __float128 is architecture-specific, there isn't a preprocessor conditional that means "__float128 is available" (whereas one could test __FLT128_MANT_DIG__ to see if _Float128 is available; __SIZEOF_FLOAT128__ is available on x86 only). But I believe the only case that actually has an alignment problem here is 32-bit x86, and already has lots of conditional specific to particular architectures or OSes, so this patch uses a conditional on __i386__; that also is the minimal change that ensures neither size nor alignment of max_align_t is changed in any case other than where it is necessary. If any other architectures turn out to have such an issue, it will show up as failures of one of the testcases added by this patch. Such an increase is of course an ABI change, but a reasonably safe one, in that max_align_t doesn't tend to appear in library interfaces (rather, it's something to use when writing allocators and similar code; most matches found on codesearch.debian.net look like copies of the gnulib stddef.h module rather than anything actually using max_align_t at all). max_align_t_align has a corresponding change (adding _Float128 to the types considered). (I think glibc malloc alignment should also increase to 16-byte on 32-bit x86 so it works for allocating objects of these types, which is now straightforward given the fix made for 32-bit powerpc.) Bootstrapped with no regressions on x86_64-pc-linux-gnu, and spot-tested with -m32 that the new float128-align.c test now compiles where previously it didn't. gcc: * ginclude/stddef.h (max_align_t) [__i386__]: Add __float128 element. gcc/c-family: * c-common.c (max_align_t_align): Also consider alignment of float128_type_node. gcc/testsuite: * gcc.dg/float128-align.c, gcc.dg/float128x-align.c, gcc.dg/float16-align.c, gcc.dg/float32-align.c, gcc.dg/float32x-align.c, gcc.dg/float64-align.c, gcc.dg/float64x-align.c, gcc.dg/floatn-align.h: New tests. From-SVN: r240248 --- gcc/ChangeLog | 5 +++++ gcc/c-family/ChangeLog | 5 +++++ gcc/c-family/c-common.c | 7 +++++-- gcc/ginclude/stddef.h | 8 ++++++++ gcc/testsuite/ChangeLog | 7 +++++++ gcc/testsuite/gcc.dg/float128-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float128x-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float16-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float32-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float32x-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float64-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/float64x-align.c | 9 +++++++++ gcc/testsuite/gcc.dg/floatn-align.h | 18 ++++++++++++++++++ 13 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/float128-align.c create mode 100644 gcc/testsuite/gcc.dg/float128x-align.c create mode 100644 gcc/testsuite/gcc.dg/float16-align.c create mode 100644 gcc/testsuite/gcc.dg/float32-align.c create mode 100644 gcc/testsuite/gcc.dg/float32x-align.c create mode 100644 gcc/testsuite/gcc.dg/float64-align.c create mode 100644 gcc/testsuite/gcc.dg/float64x-align.c create mode 100644 gcc/testsuite/gcc.dg/floatn-align.h diff --git a/gcc/ChangeLog b/gcc/ChangeLog index bc0be5ca932..e3ec20e9020 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2016-09-19 Joseph Myers + + * ginclude/stddef.h (max_align_t) [__i386__]: Add __float128 + element. + 2016-09-19 Vladimir Makarov PR rtl-optimization/77416 diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index fa9b96140fd..41afbc5af9b 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2016-09-19 Joseph Myers + + * c-common.c (max_align_t_align): Also consider alignment of + float128_type_node. + 2016-09-15 Jason Merrill * c-common.c (check_cxx_fundamental_alignment_constraints): Check diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index fc256867ec5..b6545c08105 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -12850,8 +12850,11 @@ scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, unsigned max_align_t_align () { - return MAX (TYPE_ALIGN (long_long_integer_type_node), - TYPE_ALIGN (long_double_type_node)); + unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node), + TYPE_ALIGN (long_double_type_node)); + if (float128_type_node != NULL_TREE) + max_align = MAX (max_align, TYPE_ALIGN (float128_type_node)); + return max_align; } /* Return true iff ALIGN is an integral constant that is a fundamental diff --git a/gcc/ginclude/stddef.h b/gcc/ginclude/stddef.h index d711530d053..ced2d8555ee 100644 --- a/gcc/ginclude/stddef.h +++ b/gcc/ginclude/stddef.h @@ -426,6 +426,14 @@ typedef __WINT_TYPE__ wint_t; typedef struct { long long __max_align_ll __attribute__((__aligned__(__alignof__(long long)))); long double __max_align_ld __attribute__((__aligned__(__alignof__(long double)))); + /* _Float128 is defined as a basic type, so max_align_t must be + sufficiently aligned for it. This code must work in C++, so we + use __float128 here; that is only available on some + architectures, but only on i386 is extra alignment needed for + __float128. */ +#ifdef __i386__ + __float128 __max_align_f128 __attribute__((__aligned__(__alignof(__float128)))); +#endif } max_align_t; #endif #endif /* C11 or C++11. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 29763fd1a16..741ec970054 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2016-09-19 Joseph Myers + + * gcc.dg/float128-align.c, gcc.dg/float128x-align.c, + gcc.dg/float16-align.c, gcc.dg/float32-align.c, + gcc.dg/float32x-align.c, gcc.dg/float64-align.c, + gcc.dg/float64x-align.c, gcc.dg/floatn-align.h: New tests. + 2016-09-19 Vladimir Makarov PR rtl-optimization/77416 diff --git a/gcc/testsuite/gcc.dg/float128-align.c b/gcc/testsuite/gcc.dg/float128-align.c new file mode 100644 index 00000000000..511540b7bdb --- /dev/null +++ b/gcc/testsuite/gcc.dg/float128-align.c @@ -0,0 +1,9 @@ +/* Test _Float128 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float128 } */ +/* { dg-require-effective-target float128 } */ + +#define WIDTH 128 +#define EXT 0 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float128x-align.c b/gcc/testsuite/gcc.dg/float128x-align.c new file mode 100644 index 00000000000..45b3e5b265f --- /dev/null +++ b/gcc/testsuite/gcc.dg/float128x-align.c @@ -0,0 +1,9 @@ +/* Test _Float128 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float128x } */ +/* { dg-require-effective-target float128x } */ + +#define WIDTH 128 +#define EXT 1 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float16-align.c b/gcc/testsuite/gcc.dg/float16-align.c new file mode 100644 index 00000000000..90e6f455c87 --- /dev/null +++ b/gcc/testsuite/gcc.dg/float16-align.c @@ -0,0 +1,9 @@ +/* Test _Float16 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float16 } */ +/* { dg-require-effective-target float16 } */ + +#define WIDTH 16 +#define EXT 0 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float32-align.c b/gcc/testsuite/gcc.dg/float32-align.c new file mode 100644 index 00000000000..c881700a203 --- /dev/null +++ b/gcc/testsuite/gcc.dg/float32-align.c @@ -0,0 +1,9 @@ +/* Test _Float32 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float32 } */ +/* { dg-require-effective-target float32 } */ + +#define WIDTH 32 +#define EXT 0 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float32x-align.c b/gcc/testsuite/gcc.dg/float32x-align.c new file mode 100644 index 00000000000..da396c67e5e --- /dev/null +++ b/gcc/testsuite/gcc.dg/float32x-align.c @@ -0,0 +1,9 @@ +/* Test _Float32 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float32x } */ +/* { dg-require-effective-target float32x } */ + +#define WIDTH 32 +#define EXT 1 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float64-align.c b/gcc/testsuite/gcc.dg/float64-align.c new file mode 100644 index 00000000000..82266e33fc7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/float64-align.c @@ -0,0 +1,9 @@ +/* Test _Float64 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float64 } */ +/* { dg-require-effective-target float64 } */ + +#define WIDTH 64 +#define EXT 0 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/float64x-align.c b/gcc/testsuite/gcc.dg/float64x-align.c new file mode 100644 index 00000000000..8916071ceba --- /dev/null +++ b/gcc/testsuite/gcc.dg/float64x-align.c @@ -0,0 +1,9 @@ +/* Test _Float64 alignment. */ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-add-options float64x } */ +/* { dg-require-effective-target float64x } */ + +#define WIDTH 64 +#define EXT 1 +#include "floatn-align.h" diff --git a/gcc/testsuite/gcc.dg/floatn-align.h b/gcc/testsuite/gcc.dg/floatn-align.h new file mode 100644 index 00000000000..3196f330662 --- /dev/null +++ b/gcc/testsuite/gcc.dg/floatn-align.h @@ -0,0 +1,18 @@ +/* Tests for _FloatN / _FloatNx types: test max_align_t alignment. + Before including this file, define WIDTH as the value N; define EXT + to 1 for _FloatNx and 0 for _FloatN. */ + +#define CONCATX(X, Y) X ## Y +#define CONCAT(X, Y) CONCATX (X, Y) +#define CONCAT3(X, Y, Z) CONCAT (CONCAT (X, Y), Z) + +#if EXT +# define TYPE CONCAT3 (_Float, WIDTH, x) +#else +# define TYPE CONCAT (_Float, WIDTH) +#endif + +#include + +_Static_assert (_Alignof (max_align_t) >= _Alignof (TYPE), + "max_align_t must be at least as aligned as _Float* types"); -- 2.30.2