On 26/03/15 13:21 +0000, Jonathan Wakely wrote:
This includes your fix to avoid decreasing alignment, but I didn't add a test for that as I couldn't make it fail on any of the targets I test on.
commit f796769ad20c0353490b9f1a7e019e2f0c1771fb Author: Jonathan Wakely <jwak...@redhat.com> Date: Wed Sep 3 15:39:53 2014 +0100 PR libstdc++/62259 PR libstdc++/65147 * include/std/atomic (atomic<T>): Increase alignment for types with the same size as one of the integral types. * testsuite/29_atomics/atomic/60695.cc: Adjust dg-error line number. * testsuite/29_atomics/atomic/62259.cc: New.
My patch was not sufficient to fix 65147, because I didn't increase the alignment of the std::atomic<integral type> specializations, and std::atomic<16-byte type> is only aligned correctly if __int128 is supported, which isn't true on x86 and other 32-bit targets. This is the best I've come up with, does anyone have any better ideas than the #else branch to hardcode alignment of 16-byte types to 16?
commit d0ccfb0523066c69f3d22d9cdd617a139c57f9e1 Author: Jonathan Wakely <jwak...@redhat.com> Date: Mon Mar 30 14:28:01 2015 +0100 PR libstdc++/65147 * include/bits/atomic_base.h (__atomic_base): Align as underlying type. * include/std/atomic (atomic<T>): Hardcode alignment for 16-byte types when __int128 is not available. * testsuite/29_atomics/atomic/60695.cc: Adjust dg-error line number. * testsuite/29_atomics/atomic/65147.cc: New. diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index 8104c98..48931ac 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -235,7 +235,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // 8 bytes, since that is what GCC built-in functions for atomic // memory access expect. template<typename _ITp> - struct __atomic_base + struct alignas(_ITp) __atomic_base { private: typedef _ITp __int_type; @@ -559,7 +559,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Partial specialization for pointer types. template<typename _PTp> - struct __atomic_base<_PTp*> + struct alignas(_PTp*) __atomic_base<_PTp*> { private: typedef _PTp* __pointer_type; diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic index 88c8b17..2b09477 100644 --- a/libstdc++-v3/include/std/atomic +++ b/libstdc++-v3/include/std/atomic @@ -175,6 +175,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : sizeof(_Tp) == sizeof(long long) ? alignof(long long) #ifdef _GLIBCXX_USE_INT128 : sizeof(_Tp) == sizeof(__int128) ? alignof(__int128) +#else + : sizeof(_Tp) == 16 ? 16 #endif : 0; diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc b/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc index 6f618a0..f755be0 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc @@ -27,4 +27,4 @@ struct X { char stuff[0]; // GNU extension, type has zero size }; -std::atomic<X> a; // { dg-error "not supported" "" { target *-*-* } 189 } +std::atomic<X> a; // { dg-error "not supported" "" { target *-*-* } 191 } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc b/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc new file mode 100644 index 0000000..bb92513 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc @@ -0,0 +1,42 @@ +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +// PR libstdc++65147 + +#include <atomic> + +static_assert( alignof(std::atomic<short>) == alignof(short), + "atomic short must be aligned like short" ); + +static_assert( alignof(std::atomic<int>) == alignof(int), + "atomic int must be aligned like int" ); + +static_assert( alignof(std::atomic<long>) == alignof(long), + "atomic long must be aligned like long" ); + +static_assert( alignof(std::atomic<long long>) == alignof(long long), + "atomic long long must be aligned like long long" ); + +struct S { + char s[16]; +}; + +static_assert( alignof(std::atomic<S>) > 1, + "atomic 16-byte struct must not be aligned like char" );