On Wed, Mar 19, 2025 at 12:11:56PM -0700, Andi Kleen wrote:
> From: Andi Kleen <a...@gcc.gnu.org>
> 
> When -fprofile-generate is used musttail often fails because the
> compiler adds instrumentation after the tail calls.
> 
> This patch prevents adding exit extra edges after musttail because for a
> tail call the execution leaves the function and can never come back
> even on a unwind or exception.
> 
> This is only done for musttail. In principle it could be done
> for any tail calls, but the problem is that there might be other reasons
> why the tail fails later, and then the edge might be still needed.
> 
> Passes bootstrap and full testing on x86_64 with no new failures.
> 
> Co-authored-by: Andrew Pinski
> 
>       PR 118442

118442 should be prefixed by PR component
> 
> gcc/ChangeLog:
> 
>       * tree-cfg.cc (stmt_can_terminate_bb_p):

Description missing.

> --- a/gcc/tree-cfg.cc
> +++ b/gcc/tree-cfg.cc
> @@ -8871,6 +8871,13 @@ stmt_can_terminate_bb_p (gimple *t)
>        edge e;
>        basic_block bb;
>  
> +      /* When it's a enforced tail call there is no edge because
> +      the control flow has left the function and can never come back.
> +      This prevents instrumenting the edge which would make the must
> +      tail call fail.  */
> +      if (gimple_call_must_tail_p (as_a <const gcall *> (t)))
> +     return false;
> +
>        if (call_flags & (ECF_PURE | ECF_CONST)
>         && !(call_flags & ECF_LOOPING_CONST_OR_PURE))
>       return false;

This looks wrong to me.  Even tail calls can be terminated with exit,
perform longjmp, do other things for which stmt_can_terminate_bb_p
should return true.  stmt_can_terminate_bb_p is used in many places, not
just in the predict instrumentation.
IMHO all we should ensure is that instrumentation isn't added after such
calls.

        Jakub

Reply via email to