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

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