public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tkoeppe at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/66881] New: Possibly inefficient std::atomic<int> codegen on x86 for simple arithmetic
Date: Wed, 15 Jul 2015 16:05:00 -0000	[thread overview]
Message-ID: <bug-66881-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 66881
           Summary: Possibly inefficient std::atomic<int> codegen on x86
                    for simple arithmetic
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoeppe at google dot com
  Target Milestone: ---

Consider these two simple versions of addition:

  #include <atomic>

  std::atomic<int> x;
  int y;

  void f(int a) {
    x.store(x.load(std::memory_order_relaxed) + a, std::memory_order_relaxed);
  }

  void g(int a) {
    y += a;
  }

GCC generates the following assembly:

  f(int):
        mov     eax, DWORD PTR x[rip]
        add     edi, eax
        mov     DWORD PTR x[rip], edi
        ret

  g(int):
        add     DWORD PTR y[rip], edi
        ret

Now, it is clear to me that the correct atomic codegen for store() and load()
is "mov", as it appears here, but why aren't the two consecutive operations not
folded into a single add? Aren't the semantics and the memory ordering the
same? x86 says that (most) "reads" and "writes" are strongly ordered; doesn't
that apply to the read and write produced by "add", too?

(My original motivation came from a variant of this with floats, where the
non-atomic code executed noticeably faster, even though I would have expected
the two to produce the same machine code.)


             reply	other threads:[~2015-07-15 16:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-15 16:05 tkoeppe at google dot com [this message]
2015-07-15 16:23 ` [Bug middle-end/66881] " hjl.tools at gmail dot com
2015-07-15 16:29 ` ville.voutilainen at gmail dot com
2015-07-15 16:30 ` hjl.tools at gmail dot com
2015-07-15 16:37 ` hjl.tools at gmail dot com
2015-07-15 16:37 ` hjl.tools at gmail dot com
2015-07-20 21:57 ` jfb at chromium dot 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-66881-4@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).