]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Implement flat loop profile detection
authorJan Hubicka <jh@suse.cz>
Fri, 21 Jul 2023 15:34:31 +0000 (17:34 +0200)
committerJan Hubicka <jh@suse.cz>
Fri, 21 Jul 2023 15:34:31 +0000 (17:34 +0200)
This patch adds maybe_flat_loop_profile which can be used in loop profile udpate
to detect situation where the profile may be unrealistically flat and should
not be dwonscalled after vectorizing, unrolling and other transforms that
assume that loop has high iteration count even if the CFG profile says
otherwise.

Profile is flat if it was statically detected and at that time we had
no idea about actual number of iterations or we artificially capped them.
So the function considers flat all profiles that have guessed or lower
reliability in their count and there is no nb_iteration_bounds/estimate
which would prove that the profile iteration count is high enough.

gcc/ChangeLog:

* cfgloop.h (maybe_flat_loop_profile): Declare
* cfgloopanal.cc (maybe_flat_loop_profile): New function.
* tree-cfg.cc (print_loop_info): Print info about flat profiles.

gcc/cfgloop.h
gcc/cfgloopanal.cc
gcc/tree-cfg.cc

index 269694c7962494c3cfbe3c5facee06efd863f21c..22293e1c23741d0af9f9ce53e154410f3e0932c3 100644 (file)
@@ -407,6 +407,7 @@ gcov_type expected_loop_iterations_unbounded (const class loop *,
 extern bool expected_loop_iterations_by_profile (const class loop *loop,
                                                 sreal *ret,
                                                 bool *reliable = NULL);
+extern bool maybe_flat_loop_profile (const class loop *);
 extern unsigned expected_loop_iterations (class loop *);
 extern rtx doloop_condition_get (rtx_insn *);
 
index c86a537f02458c4af3ac66dc49377d33b9179eed..d8923b27e5d50f0fb07b4523c8d845a160edb1c9 100644 (file)
@@ -303,6 +303,67 @@ expected_loop_iterations_by_profile (const class loop *loop, sreal *ret,
   return true;
 }
 
+/* Return true if loop CFG profile may be unrealistically flat.
+   This is a common case, since average loops iterate only about 5 times.
+   In the case we do not have profile feedback or do not know real number of
+   iterations during profile estimation, we are likely going to predict it with
+   similar low iteration count.  For static loop profiles we also artificially
+   cap profile of loops with known large iteration count so they do not appear
+   significantly more hot than other loops with unknown iteration counts.
+
+   For loop optimization heuristics we ignore CFG profile and instead
+   use get_estimated_loop_iterations API which returns estimate
+   only when it is realistic.  For unknown counts some optimizations,
+   like vectorizer or unroller make guess that iteration count will
+   be large.  In this case we need to avoid scaling down the profile
+   after the loop transform.  */
+
+bool
+maybe_flat_loop_profile (const class loop *loop)
+{
+  bool reliable;
+  sreal ret;
+
+  if (!expected_loop_iterations_by_profile (loop, &ret, &reliable))
+    return true;
+
+  /* Reliable CFG estimates ought never be flat.  Sanity check with
+     nb_iterations_estimate.  If those differ, it is a but in profile
+     updating code  */
+  if (reliable)
+    {
+      int64_t intret = ret.to_nearest_int ();
+      if (loop->any_estimate
+         && (wi::ltu_p (intret * 2, loop->nb_iterations_estimate)
+             || wi::gtu_p (intret, loop->nb_iterations_estimate * 2)))
+       {
+         if (dump_file && (dump_flags & TDF_DETAILS))
+           fprintf (dump_file,
+                   "Loop %i has inconsistent iterations estimates: "
+                   "reliable CFG based iteration estimate is %f "
+                   "while nb_iterations_estimate is %i\n",
+                   loop->num,
+                   ret.to_double (),
+                   (int)loop->nb_iterations_estimate.to_shwi ());
+         return true;
+       }
+      return false;
+    }
+
+  /* Allow some margin of error and see if we are close to known bounds.
+     sreal (9,-3) is 9/8  */
+  int64_t intret = (ret * sreal (9, -3)).to_nearest_int ();
+  if (loop->any_upper_bound && wi::geu_p (intret, loop->nb_iterations_upper_bound))
+    return false;
+  if (loop->any_likely_upper_bound
+      && wi::geu_p (intret, loop->nb_iterations_likely_upper_bound))
+    return false;
+  if (loop->any_estimate
+      && wi::geu_p (intret, loop->nb_iterations_estimate))
+    return false;
+  return true;
+}
+
 /* Returns expected number of iterations of LOOP, according to
    measured or guessed profile.
 
index a6c97a0466280a7d9cab3c281b21b8038e8f9b2d..c65af8cc8003d1930d4dbccc11669fde18d2cdd2 100644 (file)
@@ -8523,8 +8523,11 @@ print_loop_info (FILE *file, const class loop *loop, const char *prefix)
   bool reliable;
   sreal iterations;
   if (loop->num && expected_loop_iterations_by_profile (loop, &iterations, &reliable))
-    fprintf (file, "\n%siterations by profile: %f %s", prefix,
-            iterations.to_double (), reliable ? "(reliable)" : "(unreliable)");
+    {
+      fprintf (file, "\n%siterations by profile: %f (%s%s)", prefix,
+              iterations.to_double (), reliable ? "reliable" : "unreliable",
+              maybe_flat_loop_profile (loop) ? ", maybe flat" : "");
+    }
 
 }