public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lh_mouse at 126 dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/105495] `__atomic_compare_exchange` prevents tail-call optimization
Date: Fri, 06 May 2022 07:40:33 +0000	[thread overview]
Message-ID: <bug-105495-4-PSFkeqhpkP@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105495-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2022-05-06  7:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-05 14:00 [Bug c/105495] New: " lh_mouse at 126 dot com
2022-05-05 14:10 ` [Bug c/105495] " lh_mouse at 126 dot com
2022-05-05 14:17 ` rguenth at gcc dot gnu.org
2022-05-05 14:30 ` lh_mouse at 126 dot com
2022-05-05 14:33 ` jakub at gcc dot gnu.org
2022-05-05 15:28 ` [Bug middle-end/105495] " pinskia at gcc dot gnu.org
2022-05-06  7:40 ` lh_mouse at 126 dot com [this message]
2022-10-31  7:55 ` [Bug tree-optimization/105495] " pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105495-4-PSFkeqhpkP@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).