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