On Thu, Mar 20, 2025 at 10:01:02AM -0700, Andi Kleen wrote:
> So it could be as simple as that patch?  It solves your test case at least
> for x86.

Not sure I like this, but if others (e.g. Richi, Joseph, Jason) are ok with
it I can live with it.  But we'd need a good documentation, ideally some
some new warning about it (perhaps enabled in -Wextra) and testcase
coverage.
Looking around, clang warns for
int foo (int *);
int bar (int *x)
{
  *x = 42;
  int a = 0;
  [[clang::musttail]] return foo (&a);
}
by default (without -Wall/-Wextra) with
warning: address of stack memory associated with local variable 'a' passed to 
musttail function [-Wreturn-stack-address]
Dunno if that warning is about other stuff or just that.
If just that, perhaps we'd need multiple levels for it, diagnose passing
those to musttail arguments by default and have in -Wextra stronger variant
that would diagnose even the
int baz (int *x)
{
  *x = 42;
  int a = 0;
  foo (&a);
  [[clang::musttail]] return foo (nullptr);
}
case, i.e. escaped variables that are still in scope.
That variant of the warning perhaps should suggest to users to end the
lifetime of those variables before the musttail call.

> --- a/gcc/tree-tailcall.cc
> +++ b/gcc/tree-tailcall.cc
> @@ -722,8 +722,9 @@ find_tail_calls (basic_block bb, struct tailcall **ret, 
> bool only_musttail,
>             {
>               if (local_live_vars)
>                 BITMAP_FREE (local_live_vars);
> -             maybe_error_musttail (call,
> -                                   _("call invocation refers to locals"));
> +             /* Allow this for musttail to match clang semantics of 
> musttail.  */
> +             if (gimple_call_must_tail_p (call))
> +               continue;
>               return;
>             }
>           else
> @@ -732,8 +733,9 @@ find_tail_calls (basic_block bb, struct tailcall **ret, 
> bool only_musttail,
>               if (bitmap_bit_p (local_live_vars, *v))
>                 {
>                   BITMAP_FREE (local_live_vars);
> -                 maybe_error_musttail (call,
> -                                       _("call invocation refers to 
> locals"));
> +                 /* Allow this for musttail to match clang semantics of 
> musttail.  */
> +                 if (gimple_call_must_tail_p (call))
> +                   continue;
>                   return;
>                 }
>             }

        Jakub

Reply via email to