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

            Bug ID: 88456
           Summary: __atomic_compare_exchange implementation
                    inconsistently used
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick at motec dot com.au
  Target Milestone: ---

While implementing atomic support for an embedded target I came across some
unexpected behaviour in gcc.

In the following example:

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>

bool
__atomic_compare_exchange_1(uint8_t *p, uint8_t *e, uint8_t d, bool w, int sm,
int fm)
{
        asm volatile("nop");
        return false;
}

int main()
{
        uint8_t x, e, d;
        atomic_compare_exchange_strong_explicit(&x, &e, d, 0, 0);

        return 0;
}

The provided __atomic_compare_exchange_1 implementation is only used at -O2 or
greater:

% gcc -c -O0 test.c; objdump -d test.o |grep cmpxchg          
  60:   f0 40 0f b0 31          lock cmpxchg %sil,(%rcx)
% gcc -c -O1 test.c; objdump -d test.o |grep cmpxchg
  20:   f0 0f b0 4c 24 07       lock cmpxchg %cl,0x7(%rsp)
% gcc -c -O2 test.c; objdump -d test.o |grep cmpxchg
% gcc -c -O3 test.c; objdump -d test.o |grep cmpxchg

This was observed with gcc 8.2.1:

% gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --enable-libmpx --with-system-zlib --with-isl
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu
--disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object
--enable-linker-build-id --enable-lto --enable-plugin
--enable-install-libiberty --with-linker-hash-style=gnu
--enable-gnu-indirect-function --enable-multilib --disable-werror
--enable-checking=release --enable-default-pie --enable-default-ssp
--enable-cet=auto
Thread model: posix
gcc version 8.2.1 20181127 (GCC)

Reply via email to