public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jan Hubicka <hubicka@ucw.cz>
To: Richard Biener <rguenther@suse.de>
Cc: Jakub Jelinek <jakub@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] tree-optimization/106912 - IPA profile and pure/const
Date: Fri, 17 Mar 2023 20:40:34 +0100	[thread overview]
Message-ID: <ZBTCMrFx87T38eQR@kam.mff.cuni.cz> (raw)
In-Reply-To: <nycvar.YFH.7.77.849.2303161410290.18795@jbgna.fhfr.qr>

> 
> The following is what I profile-bootstrapped and tested on 
> x86_64-unknown-linux-gnu.
> 
> Richard.
> 
> From d438a0d84cafced85c90204cba81de0f60ad0073 Mon Sep 17 00:00:00 2001
> From: Richard Biener <rguenther@suse.de>
> Date: Thu, 16 Mar 2023 13:51:19 +0100
> Subject: [PATCH] tree-optimization/106912 - clear const attribute from fntype
> To: gcc-patches@gcc.gnu.org
> 
> The following makes sure that after clearing pure/const from
> instrumented function declarations we are adjusting call statements
> fntype as well to handle indirect calls and because gimple_call_flags
> looks at both decl and fntype.
> 
> Like the pure/const flag clearing on decls we refrain from touching
> calls to known functions that do not have a body in the current TU.
> 
> 	PR tree-optimization/106912
> 	* tree-profile.cc (tree_profiling): Update stmts only when
> 	profiling or testing coverage.  Make sure to update calls
> 	fntype, stripping 'const' there.
> 
> 	* gcc.dg/profile-generate-4.c: New testcase.
> ---
>  gcc/testsuite/gcc.dg/profile-generate-4.c | 19 ++++++++++++
>  gcc/tree-profile.cc                       | 38 +++++++++++++++++------
>  2 files changed, 47 insertions(+), 10 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/profile-generate-4.c
> 
> diff --git a/gcc/testsuite/gcc.dg/profile-generate-4.c b/gcc/testsuite/gcc.dg/profile-generate-4.c
> new file mode 100644
> index 00000000000..c2b999fe4cb
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/profile-generate-4.c
> @@ -0,0 +1,19 @@
> +/* PR106912 */
> +/* { dg-require-profiling "-fprofile-generate" } */
> +/* { dg-options "-O2 -fprofile-generate -ftree-vectorize" } */
> +
> +__attribute__ ((__simd__))
> +__attribute__ ((__nothrow__ , __leaf__ , __const__, __noinline__))
> +double foo (double x);
> +
> +void bar(double *f, int n)
> +{
> +  int i;
> +  for (i = 0; i < n; i++)
> +    f[i] = foo(f[i]);
> +}
> +
> +double foo(double x)
> +{
> +  return x * x / 3.0;
> +}
> diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc
> index ea9d7a23443..7854cd4bc31 100644
> --- a/gcc/tree-profile.cc
> +++ b/gcc/tree-profile.cc
> @@ -835,16 +835,34 @@ tree_profiling (void)
>  
>        push_cfun (DECL_STRUCT_FUNCTION (node->decl));
>  
> -      FOR_EACH_BB_FN (bb, cfun)
> -	{
> -	  gimple_stmt_iterator gsi;
> -	  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
> -	    {
> -	      gimple *stmt = gsi_stmt (gsi);
> -	      if (is_gimple_call (stmt))
> -		update_stmt (stmt);
> -	    }
> -	}
> +      if (profile_arc_flag || flag_test_coverage)
> +	FOR_EACH_BB_FN (bb, cfun)
> +	  {
> +	    gimple_stmt_iterator gsi;
> +	    for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
> +	      {
> +		gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi));
> +		if (!call)
> +		  continue;
> +
> +		/* We do not clear pure/const on decls without body.  */
> +		tree fndecl = gimple_call_fndecl (call);
> +		if (fndecl && !gimple_has_body_p (fndecl))
> +		  continue;
> +
> +		/* Drop the const attribute from the call type (the pure
> +		   attribute is not available on types).  */
> +		tree fntype = gimple_call_fntype (call);
> +		if (fntype && TYPE_READONLY (fntype))
> +		  gimple_call_set_fntype
> +		    (call, build_qualified_type (fntype, (TYPE_QUALS (fntype)
> +							  & ~TYPE_QUAL_CONST)));

Sorry, now I am bit confused on why Jakub's fix did not need similar
fixup.  The flag is only set during the profiling stage and thus I would
expect it to still run into the problem that VOPs are missing.
Is it only becuase we do not sanity check?

Here is a testcase that shows the problem:

include <math.h>
float c;

__attribute__ ((const))
float
instrument(float v)
{
        return sin (v);
}
__attribute__ ((no_profile_instrument_function,const,noinline))
float noinstrument (float v)
{
        return instrument (v);
}

m()
{
        c+=instrument(c);
        if (!__builtin_expect (c,1))
        {
          c+=noinstrument (c);
        }
        c+=instrument(c);
}
main()
{
        m();
}

Compiling 
gcc -O0 t.c -fprofile-arcs -fno-early-inlining --coverage -lm -ftest-coverage -S ; gcc t.s -ftest-coverage -lm -fprofile-arcs
makes gcov to report 3 executions on instrument while with -O3 it is 2.

This is because the noinstrument has const flag that is not cleared and
it becoames essentially non-const because it calls instrument that gets
intrumented and partially inlined.  

Honza
> +
> +		/* Update virtual operands of calls to no longer const/pure
> +		   functions.  */
> +		update_stmt (call);
> +	      }
> +	  }
>  
>        /* re-merge split blocks.  */
>        cleanup_tree_cfg ();
> -- 
> 2.35.3
> 

  parent reply	other threads:[~2023-03-17 19:40 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-25  7:59 Richard Biener
2022-11-25 10:05 ` Jan Hubicka
2022-11-25 12:05   ` Richard Biener
2022-11-25 12:11     ` Jan Hubicka
2022-11-25 13:05       ` Richard Biener
2022-11-25 13:18         ` Jan Hubicka
2022-11-25 20:26           ` Richard Biener
2023-03-16 11:21             ` Jakub Jelinek
2023-03-16 12:05               ` Richard Biener
2023-03-16 12:13                 ` Jakub Jelinek
2023-03-16 12:22                   ` Richard Biener
2023-03-16 14:11                     ` Richard Biener
2023-03-16 14:27                       ` Jakub Jelinek
2023-03-17 19:40                       ` Jan Hubicka [this message]
2023-03-17 19:54                         ` Jakub Jelinek
2023-03-20  8:33                           ` Richard Biener
2023-03-24 10:25                             ` Richard Biener
2023-03-24 11:49                             ` Jan Hubicka
2023-03-24 13:05                       ` Jan Hubicka
2023-03-24 13:07                         ` Richard Biener
2023-03-24 13:18                           ` Jan Hubicka
2023-03-17 19:09                   ` Jan Hubicka
2023-03-17 19:27                     ` Jakub Jelinek

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=ZBTCMrFx87T38eQR@kam.mff.cuni.cz \
    --to=hubicka@ucw.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=rguenther@suse.de \
    /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).