public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jan Hubicka <hubicka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5565] Compare guessed and feedback frequencies during profile feedback stream-in
Date: Sun, 28 Nov 2021 18:43:18 +0000 (GMT)	[thread overview]
Message-ID: <20211128184318.AA3183858C27@sourceware.org> (raw)

https://gcc.gnu.org/g:2899d49e3701a4df18a336a680a7095cc99a2229

commit r12-5565-g2899d49e3701a4df18a336a680a7095cc99a2229
Author: Jan Hubicka <jh@suse.cz>
Date:   Sun Nov 28 19:42:45 2021 +0100

    Compare guessed and feedback frequencies during profile feedback stream-in
    
    This patch adds simple code to dump and compare frequencies of basic blocks
    read from the profile feedback and frequencies guessed statically.
    It dumps basic blocks in the order of decreasing frequencies from feedback
    along with guessed frequencies and histograms.
    
    It makes it to possible spot basic blocks in hot regions that are considered
    cold by guessed profile or vice versa.
    
    I am trying to figure out how realistic our profile estimate is compared to
    read one on exchange2 (looking again into PR98782.  There IRA now places spills
    into hot regions of code while with older (and worse) profile it did not.
    Catch is that the function is very large and has 9 nested loops, so it is hard
    to figure out how to improve the profile estimate and/or IRA.
    
    gcc/ChangeLog:
    
    2021-11-28  Jan Hubicka  <hubicka@ucw.cz>
    
            * profile.c: Include sreal.h
            (struct bb_stats): New.
            (cmp_stats): New function.
            (compute_branch_probabilities): Output bb stats.

Diff:
---
 gcc/profile.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 73 insertions(+), 5 deletions(-)

diff --git a/gcc/profile.c b/gcc/profile.c
index d07002d265e..dbf42ff7b2b 100644
--- a/gcc/profile.c
+++ b/gcc/profile.c
@@ -64,6 +64,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-cfg.h"
 #include "dumpfile.h"
 #include "cfgloop.h"
+#include "sreal.h"
 
 #include "profile.h"
 
@@ -385,6 +386,30 @@ read_profile_edge_counts (gcov_type *exec_counts)
     return num_edges;
 }
 
+/* BB statistics comparing guessed frequency of BB with feedback.  */
+
+struct bb_stats
+{
+  basic_block bb;
+  double guessed, feedback;
+  int64_t count;
+};
+
+/* Compare limit_tuple intervals by first item in descending order.  */
+
+static int
+cmp_stats (const void *ptr1, const void *ptr2)
+{
+  const bb_stats *p1 = (const bb_stats *)ptr1;
+  const bb_stats *p2 = (const bb_stats *)ptr2;
+
+  if (p1->feedback < p2->feedback)
+    return 1;
+  else if (p1->feedback > p2->feedback)
+    return -1;
+  return 0;
+}
+
 
 /* Compute the branch probabilities for the various branches.
    Annotate them accordingly.  
@@ -716,11 +741,52 @@ compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
   /* If we have real data, use them!  */
   if (bb_gcov_count (ENTRY_BLOCK_PTR_FOR_FN (cfun))
       || !flag_guess_branch_prob)
-    FOR_ALL_BB_FN (bb, cfun)
-      if (bb_gcov_count (bb) || !flag_profile_partial_training)
-        bb->count = profile_count::from_gcov_type (bb_gcov_count (bb));
-      else
-	bb->count = profile_count::guessed_zero ();
+    {
+      profile_count old_entry_cnt = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
+      auto_vec <bb_stats> stats;
+      double sum1 = 0, sum2 = 0;
+
+      FOR_ALL_BB_FN (bb, cfun)
+	{
+	  profile_count cnt = bb->count;
+	  if (bb_gcov_count (bb) || !flag_profile_partial_training)
+	    bb->count = profile_count::from_gcov_type (bb_gcov_count (bb));
+	  else
+	    bb->count = profile_count::guessed_zero ();
+	  if (dump_file && bb->index >= 0)
+	    {
+	      double freq1 = cnt.to_sreal_scale (old_entry_cnt).to_double ();
+	      double freq2 = bb->count.to_sreal_scale
+					(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count).
+				  to_double ();
+	      bb_stats stat = {bb, freq1, freq2,
+			       (int64_t) bb_gcov_count (bb)};
+	      stats.safe_push (stat);
+	      sum1 += freq1;
+	      sum2 += freq2;
+	    }
+	}
+      if (dump_file)
+	{
+	  double nsum1 = 0, nsum2 = 0;
+	  stats.qsort (cmp_stats);
+	  for (auto stat : stats)
+	    {
+	      nsum1 += stat.guessed;
+	      nsum2 += stat.feedback;
+	      fprintf (dump_file,
+		       " Basic block %4i guessed freq: %12.3f"
+		       " cummulative:%6.2f%% "
+		       " feedback freq: %12.3f cummulative:%7.2f%%"
+		       " cnt: 10%" PRId64 "\n", stat.bb->index,
+		       stat.guessed,
+		       nsum1 * 100 / sum1,
+		       stat.feedback,
+		       nsum2 * 100 / sum2,
+		       stat.count);
+	    }
+	}
+    }
   /* If function was not trained, preserve local estimates including statically
      determined zero counts.  */
   else if (profile_status_for_fn (cfun) == PROFILE_READ
@@ -755,6 +821,8 @@ compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
 
       fputc ('\n', dump_file);
       fputc ('\n', dump_file);
+
+      gimple_dump_cfg (dump_file, TDF_BLOCKS);
     }
 
   free_aux_for_blocks ();


                 reply	other threads:[~2021-11-28 18:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211128184318.AA3183858C27@sourceware.org \
    --to=hubicka@gcc.gnu.org \
    --cc=gcc-cvs@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).