public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/65147] alignment of std::atomic object is not correct
Date: Mon, 30 Mar 2015 11:42:00 -0000	[thread overview]
Message-ID: <bug-65147-4-fVdbzT3OQG@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-65147-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65147

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Alexey Lapshin from comment #7)
> It looks like this fix makes alignment of atomic object to be the same as
> alignment of integral non-atomic object of the same size.

Actually it only did that for non-integral atomic objects, e.g. I didn't do
anything to change std::atomic<long long>.

> The gcc behavior is different it makes alignment of atomic objects of sizes
> 1,2,4,8,16 to match with size :

That's not strictly true, there is a target hook (atomic_align_for_mode) which
specifies the alignment for 1/2/4/8/16-byte objects, and the result is not
necessarily the same as the size. Or so I'm told. That's why I used the nested
conditional expressions with alignof(integral type) instead of just using
alignas(sizeof(T)).

> $g++ -latomic -std=c++11 -m32 all.cc 
> $./a.out
> 
>  sizeof(ac) 1 alignof(ac) 1
>  sizeof(as) 2 alignof(as) 2
>  sizeof(al) 4 alignof(al) 4
>  sizeof(all) 8 alignof(all) 4
>  sizeof(a16) 16 alignof(a16) 1

There are two problems here.

The first is that alignof(std::atomic<long long>) is less than alignof(long
long), and my recent changes didn't address that. That is easy to fix:

--- 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;


The second problem is that alignof(struct S16) is not increased. That's because
libstdc++ doesn't support __int128 on x86, so this bit of code doesn't do
anything:

#ifdef _GLIBCXX_USE_INT128
    : sizeof(_Tp) == sizeof(__int128)  ? alignof(__int128)
#endif

I'm not sure how to fix this. Maybe we should just bodge it like this and hope
it is valid for all important targets:

#ifdef _GLIBCXX_USE_INT128
    : sizeof(_Tp) == sizeof(__int128)  ? alignof(__int128)
#else
    : sizeof(_Tp) == 16  ? 16
#endif

(The real solution is a new attribute that uses the target hook, so we can
guarantee the same result as the C front end, but it's too late to do that for
GCC 5).


  parent reply	other threads:[~2015-03-30 10:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-20 20:01 [Bug c++/65147] New: " alexey.lapshin at oracle dot com
2015-02-20 22:11 ` [Bug c++/65147] " joseph at codesourcery dot com
2015-03-20 22:15 ` [Bug libstdc++/65147] " jason at gcc dot gnu.org
2015-03-23 18:07 ` alexey.lapshin at oracle dot com
2015-03-23 18:36 ` redi at gcc dot gnu.org
2015-03-26 19:59 ` redi at gcc dot gnu.org
2015-03-26 20:24 ` redi at gcc dot gnu.org
2015-03-27 15:58 ` alexey.lapshin at oracle dot com
2015-03-27 19:15 ` redi at gcc dot gnu.org
2015-03-28 10:29 ` redi at gcc dot gnu.org
2015-03-30 11:42 ` redi at gcc dot gnu.org [this message]
2015-04-09 11:16 ` redi at gcc dot gnu.org
2015-04-09 11:18 ` redi at gcc dot gnu.org
2015-04-10 14:46 ` alexey.lapshin at oracle dot com
2015-04-22 12:02 ` jakub at gcc dot gnu.org
2015-04-22 12:59 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-65147-4-fVdbzT3OQG@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).