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(refs/users/kubaneko/heads/histogram)] Use histogram in loop vectorizer heuristics
Date: Mon,  1 May 2023 22:14:28 +0000 (GMT)	[thread overview]
Message-ID: <20230501221428.6E8AE3858D1E@sourceware.org> (raw)

https://gcc.gnu.org/g:e4287fa88d78d5569ad8948378bf7d4395b6f157

commit e4287fa88d78d5569ad8948378bf7d4395b6f157
Author: Honza <jh@ryzen3.suse.cz>
Date:   Tue May 2 00:13:52 2023 +0200

    Use histogram in loop vectorizer heuristics

Diff:
---
 gcc/tree-ssa-loop-ivcanon.cc |  1 +
 gcc/tree-vect-loop.cc        | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
index d5a10bb5aff..1fde151551b 100644
--- a/gcc/tree-ssa-loop-ivcanon.cc
+++ b/gcc/tree-ssa-loop-ivcanon.cc
@@ -1115,6 +1115,7 @@ try_peel_loop (class loop *loop,
       gcov_type rest = sum;
       gcov_type psum = 0;
       int good_percentage = param_profile_histogram_peel_prcnt;
+      /* TODO: Fix.  Profile size needs to be stored in the counters.  */
       for (int i = 0; i < param_profile_histogram_size_lin; i++)
 	{
 	  psum += (*(loop->counters->hist))[i];
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index c172b466809..140bafad28c 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -2070,6 +2070,37 @@ vect_analyze_loop_costing (loop_vec_info loop_vinfo,
       return 0;
     }
 
+  bool use_estimate = true;
+  /* With histogram we can determine how often the vectorized loop will be
+     reached.  */
+  if (loop->counters
+      /* TODO: For epilogue we can compute new histogram.  */
+      && !LOOP_VINFO_EPILOGUE_P (loop_vinfo)
+      && loop->counters->sum
+      /* TODO: Fix.  Profile size needs to be stored in the counters.  */
+      && min_profitable_estimate <= param_profile_histogram_size_lin)
+    {
+      gcov_type psum = 0;
+      for (int i = 0; i < min_profitable_estimate; i++)
+	psum += (*(loop->counters->hist))[i];
+      /* TODO: This is less precise than needed.  If histogram was also
+	 storing number of executions of the loop header (which may
+	 be different from loop->header->count if i.e. inlining happen).
+	 We could compute expected nunber of execution of the vectorized
+	 loop.  */
+      if (psum == loop->counters->sum
+	  || !maybe_hot_count_p (cfun, loop->header->count.apply_scale
+				  (loop->counters->sum - psum,
+				   loop->counters->sum)))
+	{
+	  dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+			   "not vectorized: according to loop histogram "
+			   "the vectorized loop would be cold.\n");
+	  return 0;
+	}
+      use_estimate = false;
+    }
+
   HOST_WIDE_INT estimated_niter;
 
   /* If we are vectorizing an epilogue then we know the maximum number of
@@ -2078,7 +2109,7 @@ vect_analyze_loop_costing (loop_vec_info loop_vinfo,
   if (LOOP_VINFO_EPILOGUE_P (loop_vinfo))
     estimated_niter
       = vect_vf_for_cost (LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo)) - 1;
-  else
+  else if (use_estimate)
     {
       estimated_niter = estimated_stmt_executions_int (loop);
       if (estimated_niter == -1)

                 reply	other threads:[~2023-05-01 22:14 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=20230501221428.6E8AE3858D1E@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).