From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1075) id AA3183858C27; Sun, 28 Nov 2021 18:43:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA3183858C27 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jan Hubicka To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-5565] Compare guessed and feedback frequencies during profile feedback stream-in X-Act-Checkin: gcc X-Git-Author: Jan Hubicka X-Git-Refname: refs/heads/master X-Git-Oldrev: d1471457fcda26cd24e0053b82f8eed35ddb8727 X-Git-Newrev: 2899d49e3701a4df18a336a680a7095cc99a2229 Message-Id: <20211128184318.AA3183858C27@sourceware.org> Date: Sun, 28 Nov 2021 18:43:18 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 28 Nov 2021 18:43:18 -0000 https://gcc.gnu.org/g:2899d49e3701a4df18a336a680a7095cc99a2229 commit r12-5565-g2899d49e3701a4df18a336a680a7095cc99a2229 Author: Jan Hubicka 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 * 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 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 ();