]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix handling of GUESSED_LOCAL in auto-fdo and preserve more static profile
authorJan Hubicka <hubicka@ucw.cz>
Thu, 5 Jun 2025 13:24:36 +0000 (15:24 +0200)
committerJan Hubicka <hubicka@ucw.cz>
Thu, 5 Jun 2025 13:24:36 +0000 (15:24 +0200)
This patch fixes ICE where GUESSED_LOCAL was kept in autofdo profile.
It may make more sense to turn GESSED_LOCAL 0 to GUESSED 0 since it seems
bit more informative then autofdo 0 (which really means that count is below
the 2% threshold or that info was lost due to some code transformation).

The patch also modifies code setting probabilities of edge to keep reliable
predictions of 0 or 1.

gcc/ChangeLog:

* auto-profile.cc (update_count_by_afdo_count): Fix handling
of GUESSED_LOCAL.
(afdo_calculate_branch_prob): Preserve static profile for
probabilities 0 and 1.

gcc/auto-profile.cc

index 215dadf87c21a6c2286c78a8d3120ab36e98dc13..91b1e97f5bb4c992450fa4ef21057a638b9c4ceb 100644 (file)
@@ -1120,7 +1120,8 @@ update_count_by_afdo_count (profile_count *count, gcov_type c)
   /* In case we have guessed profile which is already zero, preserve
      quality info.  */
   else if (count->nonzero_p ()
-          || count->quality () == GUESSED)
+          || count->quality () == GUESSED
+          || count->quality () == GUESSED_LOCAL)
     *count = profile_count::zero ().afdo ();
 }
 
@@ -1515,8 +1516,21 @@ afdo_calculate_branch_prob (bb_set *annotated_bb)
     if (num_unknown_succ == 0 && total_count.nonzero_p ())
       {
        FOR_EACH_EDGE (e, ei, bb->succs)
-         e->probability
-           = AFDO_EINFO (e)->get_count ().probability_in (total_count);
+         {
+           /* If probability is 1, preserve reliable static prediction
+              (This is, for example the case of single fallthru edge
+               or single fallthru plus unlikely EH edge.)  */
+           if (AFDO_EINFO (e)->get_count () == total_count ()
+               && e->probability == profile_probability::always ())
+             ;
+           else if (AFDO_EINFO (e)->get_count ().nonzero_p ())
+             e->probability
+               = AFDO_EINFO (e)->get_count ().probability_in (total_count);
+           /* If probability is zero, preserve reliable static prediction.  */
+           else if (e->probability.nonzero_p ()
+                    || e->probability.quality () == GUESSED)
+             e->probability = profile_probability::never ().afdo ();
+         }
       }
   }
   FOR_ALL_BB_FN (bb, cfun)