public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant
@ 2021-11-23  5:39 doodspav at gmail dot com
  2021-11-30  4:18 ` [Bug middle-end/103372] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: doodspav at gmail dot com @ 2021-11-23  5:39 UTC (permalink / raw)
  To: gcc-bugs

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).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
@ 2021-11-30  4:18 ` pinskia at gcc dot gnu.org
  2021-11-30  4:25 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-30  4:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

clang decides (on aarch64 at least) emits a conditional based on the argument,
instead of special casing it to SEQ_CST .

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-30  4:25 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-11-30

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So clang in this case is wrong in that there is no reason to do figure out
failure since succ is memory_order_relaxed.

In this case, with a non-constant in failure, GCC should just choise the same
as success. Likewise the opposite way around. Except for
release/memory_order_acq_rel which should get special cases.

§7.17.7.4 "The failure argument shall not be memory_order_release nor
memory_order_acq_rel. The failure argument shall be no stronger than the
success argument."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-13 14:26 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=102177

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note C++17 removed this restriction too, see PR 102177.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-12-13 17:00 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |99612
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
The patch for pr99612 submitted last week resolves this problem as well:
  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99612


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99612
[Bug 99612] Remove "#pragma GCC system_header" from atomic file to warn on
incorrect memory order

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-04 21:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103372
Bug 103372 depends on bug 99612, which changed state.

Bug 99612 Summary: Remove "#pragma GCC system_header" from atomic file to warn on incorrect memory order
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99612

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/103372] Warning on failure order defaulting to SEQ_CST if not a compile time constant
  2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
                   ` (4 preceding siblings ...)
  2022-01-04 21:00 ` msebor at gcc dot gnu.org
@ 2022-01-04 21:02 ` msebor at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-04 21:02 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.2.0, 11.2.0, 4.9.4,
                   |                            |5.5.0, 6.4.0, 7.2.0, 8.3.0,
                   |                            |9.1.0
             Status|NEW                         |RESOLVED
   Target Milestone|---                         |12.0
         Resolution|---                         |FIXED

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fixed in GCC 12.  The fix and its dependencies are too intrusive not backport
to GCC 11.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-01-04 21:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-23  5:39 [Bug c/103372] New: Warning on failure order defaulting to SEQ_CST if not a compile time constant doodspav at gmail dot com
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

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).