public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rjmccall at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/110728] should __attribute__((cleanup())) callback get invoked for indirect edges of asm goto
Date: Wed, 19 Jul 2023 17:16:55 +0000	[thread overview]
Message-ID: <bug-110728-4-KuAKVv0zQj@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110728-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #8 from John McCall <rjmccall at gmail dot com> ---
> Let me clarify.  If GCC were change behavior of `asm goto` to
> invoke the destructor/cleanup function before the backwards edge
> of `asm goto`, I would submit a patch to clang to implement that
> behavior as well.

Ah, thank you, I understand what you're saying now.  Yes, we would take that.

I think this test case might be a little more illuminating and separates out
the issue of the back-edge and the uninitialized variable:
https://godbolt.org/z/doG7414va

```
#include <malloc.h>

#define GOTO 1
#define COMPUTED_GOTO 2
#define ASM_GOTO 3

#define CASE ASM_GOTO

static void free_from(int **ptr) {
    free(*ptr);
}

int test() {
#if CASE == COMPUTED_GOTO
    void *lbl = &&label;
#endif
    if (1) {
        __attribute__((cleanup(free_from))) int *p = calloc(sizeof(int), 1);
#if CASE == GOTO
        goto label;
#elif CASE == COMPUTED_GOTO
        goto *lbl;
#elif CASE == ASM_GOTO
        asm goto("<<< asm goto %l0 >>>"::::label);
#endif
    }
    label:
    return 0;
}
```

GCC produces this output for CASE == ASM_GOTO (stripping the .LFB labels):

```
test:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     esi, 1
        mov     edi, 4
        call    calloc
        mov     QWORD PTR [rbp-8], rax
        <<< asm goto .L3 >>>
        lea     rax, [rbp-8]
        mov     rdi, rax
        call    free_from
.L3:
        mov     eax, 0
        leave
        ret
``

It does something similar for CASE == COMPUTED_GOTO.  In both cases, the branch
avoids the cleanup with no diagnostic.  But it does the right thing with CASE
== GOTO.

  parent reply	other threads:[~2023-07-19 17:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-18 21:23 [Bug c/110728] New: " ndesaulniers at google dot com
2023-07-18 21:29 ` [Bug c/110728] " pinskia at gcc dot gnu.org
2023-07-18 21:32 ` pinskia at gcc dot gnu.org
2023-07-18 21:33 ` pinskia at gcc dot gnu.org
2023-07-18 21:48 ` pinskia at gcc dot gnu.org
2023-07-18 23:22 ` rjmccall at gmail dot com
2023-07-19  7:01 ` rguenth at gcc dot gnu.org
2023-07-19 16:46 ` [Bug middle-end/110728] " ndesaulniers at google dot com
2023-07-19 17:16 ` rjmccall at gmail dot com [this message]
2023-08-03 14:38 ` matz at gcc dot gnu.org
2023-08-03 16:00 ` ndesaulniers at google dot com
2023-08-03 16:11 ` matz at gcc dot gnu.org
2023-08-03 17:07 ` rjmccall at gmail dot com

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-110728-4-KuAKVv0zQj@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).