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

            Bug ID: 118430
           Summary: musttail false positive on how locals are used
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kenjin4096 at gmail dot com
  Target Milestone: ---

Dear GCC maintainers,

The following code gives a compilation error, when compiling with GCC 15.0
(experimental) commit c1729df6ec1eff4815a9cdd71392691ce21da028, on Ubuntu 22.04
AMD64. In Clang 19.1.0, this compiles successfully, producing working code:

test.c
```
#include "stdio.h"

__attribute__((noinline))
int tail_call2(int opcode) {
    printf("%d\n", opcode);
    return 1;
}

__attribute__((noinline))
int non_tail_call(int *ptr) {
    printf("%d\n", *ptr);
    return 2;
}

int tail_call1(int opcode) {
    int val;
    printf("%d\n", opcode);
    non_tail_call(&val); // Trigger the bug by passing in a local.
    [[gnu::musttail]]
    return tail_call2(opcode);
}


int main(int argc, char **argv) {
    tail_call1(argc);
}
```

Compilation error depends on options passed.

gcc test.c gives:

test.c: In function ‘tail_call1’:
test.c:19:12: error: cannot tail-call: call invocation refers to locals
   19 |     return tail_call2(opcode);
      |            ^~~~~~~~~~~~~~~~~~

gcc -O1 test.c passes


gcc -O2/3 test.c gives:

test.c: In function ‘tail_call1’:
test.c:19:12: error: cannot tail-call: call uses return slot
   19 |     return tail_call2(opcode);
      |            ^~~~~~~~~~~~~~~~~~

Output in first case and third case don't seem correct. In the first error, the
tail call itself is not referring to a local, only a previous non-function tail
call is. In the 3rd case, I don't see how it's using a return slot: the return
value is not being assigned. I might be mistaken on what a "return slot" is in
this case as I'm not a GCC expert though.

My background use case is implementing an optimization for CPython's
interpreter. I have a PR that implements a nontrivial amount of code using
musttail. The following PR works fully on clang-19, but fails to compile on GCC
after applying patches to make it detect GCC:
https://github.com/python/cpython/pull/128718

Thank you for your time and help.

Possibly related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115979

Regards,
Ken Jin

Reply via email to