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

--- Comment #5 from LIU Hao <lh_mouse at 126 dot com> ---
This does not trigger the issue:

```c
#define __atomic_compare_exchange(p,c,n,w,ms,mf)  \
  ({ int __temp;  \
     __builtin_memcpy(&__temp, c, sizeof(*c));  \
     _Bool __r = __atomic_compare_exchange(p, (__typeof__(*(c))*) &__temp, n,
w, ms, mf);  \
     __builtin_memcpy(c, &__temp, sizeof(*c));  \
     __r;  });

typedef struct { int b; } cond;

int
__MCF_batch_release_common(cond* p, int c);

int
_MCF_cond_signal_some(cond* p, int x)
  {
    cond c = {x}, n = {2};
    __atomic_compare_exchange(p, &c, &n, 1, 0, 0);
    return __MCF_batch_release_common(p, x);
  }
```

which results in (godbolt https://gcc.godbolt.org/z/n68T1c6oP):

```asm
_MCF_cond_signal_some:
        mov     edx, 2
        mov     eax, esi
        lock cmpxchg    DWORD PTR [rdi], edx
        jmp     __MCF_batch_release_common
```

Effectively, we are using a `int` to provide storage for a struct of
`sizeof(int)`.

But if we use a `long` to provide storage for the struct, such issue reappears:

```c
#define __atomic_compare_exchange(p,c,n,w,ms,mf)  \
  ({ long __temp;  \
     __builtin_memcpy(&__temp, c, sizeof(*c));  \
     _Bool __r = __atomic_compare_exchange(p, (__typeof__(*(c))*) &__temp, n,
w, ms, mf);  \
     __builtin_memcpy(c, &__temp, sizeof(*c));  \
     __r;  });

typedef struct { int b; } cond;

int
__MCF_batch_release_common(cond* p, int c);

int
_MCF_cond_signal_some(cond* p, int x)
  {
    cond c = {x}, n = {2};
    __atomic_compare_exchange(p, &c, &n, 1, 0, 0);
    return __MCF_batch_release_common(p, x);
  }
```

which results in (https://gcc.godbolt.org/z/PGof8nGd7)

```asm
_MCF_cond_signal_some:
        mov     edx, 2
        mov     eax, esi
        mov     DWORD PTR [rsp-16], esi
        lock cmpxchg    DWORD PTR [rdi], edx
        je      .L2
        mov     DWORD PTR [rsp-16], eax
.L2:
        jmp     __MCF_batch_release_common
```

It is also notable that with this kind of hacks, GCC is finally able to perform
TCO on the return statement.

Reply via email to