public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "doodspav at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant
Date: Tue, 23 Nov 2021 05:39:34 +0000	[thread overview]
Message-ID: <bug-103372-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 103372
           Summary: Warning on failure order defaulting to SEQ_CST if not
                    a compile time constant
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: doodspav at gmail dot com
  Target Milestone: ---

The following code:

#include <stdatomic.h>

_Bool test(memory_order failure)
{
  volatile _Atomic(int) object = 5;
  int expected = 5;
  int desired = 10;
  memory_order success = memory_order_relaxed;

  return atomic_compare_exchange_strong_explicit(
    &object,
    &expected,
    desired,
    success,
    failure
  );
}

generates this error:

<source>:10:10: error: failure memory model cannot be stronger than success
memory model for '__atomic_compare_exchange' [-Werror=invalid-memory-model]
   10 |   return atomic_compare_exchange_strong_explicit(
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

when compiled on Godbolt with GCC4.9.0+ and '-std=c11 -O3 -Werror
-Winvalid-memory-model'.

GCC implements atomics using the builtins, which convert any memory order
parameter to `SEQ_CST` if it's not a compile time constant. This is fine,
except in the case of __atomic_compare_exchange(_n) where there are 2 memory
orders.

An acceptable fix would be that GCC should not warn about the failure memory
order being weaker than the success memory order if the failure order is not
known at compile time.
Since GCC sets failure order to SEQ_CST in this case, it will need to also set
success order to SEQ_CST.
This is permitted and, under the current system, is the only success order
which won't cause the above issue (any other success order would currently
warn/error).

             reply	other threads:[~2021-11-23  5:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-23  5:39 doodspav at gmail dot com [this message]
2021-11-30  4:18 ` [Bug middle-end/103372] " pinskia at gcc dot gnu.org
2021-11-30  4:25 ` pinskia at gcc dot gnu.org
2021-12-13 14:26 ` pinskia at gcc dot gnu.org
2021-12-13 17:00 ` msebor at gcc dot gnu.org
2022-01-04 21:00 ` msebor at gcc dot gnu.org
2022-01-04 21:02 ` msebor 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-103372-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).