For an unsampled edge from a sampled block, use the block count left
after its sampled outgoing edges.  Use the exact remainder for the only
unknown edge.  Otherwise distribute it by static probability.  Prefer
scales that do not depend on static probabilities.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * auto-profile.cc (afdo_estimate_edge_count): New.
        (afdo_adjust_guessed_profile): Estimate boundary edges from the
        remaining count.  Use static probability scales as a fallback.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/auto-profile.cc | 89 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 76 insertions(+), 13 deletions(-)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index d33e42bc516..eb7e6aa3f81 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -4152,6 +4152,40 @@ scale_bb_profile ()
   return false;
 }
 
+/* Estimate E from its source's remaining sampled count.  Split the count by
+   static probability when needed.  Use E's static count for inconsistent
+   samples.  Set EXACT when samples determine E.  */
+
+static profile_count
+afdo_estimate_edge_count (edge e, bool *exact)
+{
+  profile_count known = profile_count::zero ().afdo ();
+  profile_probability unknown_prob = profile_probability::never ();
+  int num_unknown = 0;
+  for (edge e2 : e->src->succs)
+    if (AFDO_EINFO (e2)->is_annotated ())
+      known += AFDO_EINFO (e2)->get_count ();
+    else
+      {
+       unknown_prob += e2->probability;
+       num_unknown++;
+      }
+  *exact = false;
+  if (known > e->src->count)
+    return e->count ();
+  profile_count remaining = e->src->count - known;
+  if (!remaining.nonzero_p () && AFDO_EINFO (e)->is_nonzero ())
+    return e->count ();
+  if (num_unknown == 1)
+    {
+      *exact = true;
+      return remaining;
+    }
+  if (!unknown_prob.nonzero_p () || !e->probability.initialized_p ())
+    return e->count ();
+  return remaining.apply_probability (e->probability / unknown_prob);
+}
+
 /* In case given basic block was fully optimized out, AutoFDO
    will have no data about it.  In this case try to preserve static profile.
    Identify connected components (in undirected form of CFG) which has
@@ -4165,6 +4199,8 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
   auto_vec <basic_block, 20> bbs (n_basic_blocks_for_fn (cfun));
   /* Scale factors found.  */
   auto_vec <scale, 20> scales;
+  /* Scales that depend on static edge probabilities.  */
+  auto_vec <scale, 20> estimated_scales;
   auto_vec <basic_block, 20> stack (n_basic_blocks_for_fn (cfun));
 
   basic_block seed_bb;
@@ -4193,6 +4229,7 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
        component_id++;
        bbs.truncate (0);
        scales.truncate (0);
+       estimated_scales.truncate (0);
        component[seed_bb->index] = component_id;
        profile_count max_count = profile_count::zero ();
 
@@ -4250,6 +4287,7 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
              component.  */
           profile_count current_component_count = profile_count::zero ();
           bool boundary = false;
+          bool estimated = false;
 
           for (edge e: b->preds)
             if (AFDO_EINFO (e)->is_annotated ())
@@ -4264,23 +4302,25 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
                 boundary = true;
                 annotated_count += AFDO_EINFO (e)->get_count ();
               }
-            /* If source is annotated, combine with static
-               probability prediction.
-               TODO: We can do better in case some of edges out are
-               annotated and distribute only remaining count out of BB.  */
+            /* Estimate from the source's remaining sampled count.  */
             else if (is_bb_annotated (e->src, *annotated_bb))
               {
                 boundary = true;
+                bool exact;
+                profile_count cnt = afdo_estimate_edge_count (e, &exact);
+                if (!exact)
+                  estimated = true;
                 if (dump_file)
                   {
                     fprintf (dump_file, "    Annotated predecessor %i "
                              "with count ", e->src->index);
                     e->src->count.dump (dump_file);
-                    fprintf (dump_file, " edge count using static profile ");
-                    e->count ().dump (dump_file);
+                    fprintf (dump_file, exact ? " remaining edge count "
+                             : " edge count using static profile ");
+                    cnt.dump (dump_file);
                     fprintf (dump_file, "\n");
                   }
-                annotated_count += e->count ();
+                annotated_count += cnt;
               }
             else
               {
@@ -4291,7 +4331,7 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
             {
               if (dump_file)
                 fprintf (dump_file, "    bb %i in count ", b->index);
-              add_scale (&scales,
+              add_scale (estimated ? &estimated_scales : &scales,
                          annotated_count,
                          b->count - current_component_count);
             }
@@ -4305,33 +4345,56 @@ afdo_adjust_guessed_profile (bb_set *annotated_bb)
               }
             else if (is_bb_annotated (e->dest, *annotated_bb))
               {
-                profile_count annotated_count = e->dest->count;
+                profile_count other_count = profile_count::zero ().afdo ();
                 profile_count out_count = profile_count::zero ();
                 bool ok = true;
+                bool estimated = false;
 
                 for (edge e2: e->dest->preds)
                   if (AFDO_EINFO (e2)->is_annotated ())
-                    annotated_count -= AFDO_EINFO (e2)->get_count ();
+                    other_count += AFDO_EINFO (e2)->get_count ();
                   else if (component[e2->src->index] == component_id)
                     out_count += e2->count ();
                   else if (is_bb_annotated (e2->src, *annotated_bb))
-                    annotated_count -= e2->count ();
+                    {
+                      bool exact;
+                      other_count += afdo_estimate_edge_count (e2, &exact);
+                      if (!exact)
+                        estimated = true;
+                    }
                   else if (e2->probability.nonzero_p ())
                     {
                       ok = false;
                       break;
                     }
-                if (!ok)
+                /* Inconsistent samples do not determine E.  */
+                if (!ok || other_count > e->dest->count)
                   continue;
+                profile_count annotated_count = e->dest->count - other_count;
                 if (dump_file)
                   fprintf (dump_file,
                            "    edge %i->%i has annotated successor; count ",
                            b->index, e->dest->index);
-                add_scale (&scales, annotated_count, e->count ());
+                add_scale (estimated ? &estimated_scales : &scales,
+                           annotated_count, e->count ());
               }
 
         }
 
+       /* Use static probability scales only as a fallback.  Ignore zero
+         scales because they provide no useful ratio.  */
+       bool nonzero_scale = false;
+       for (scale &s : scales)
+        if (s.scale > 0)
+          nonzero_scale = true;
+       if (!nonzero_scale && estimated_scales.length ())
+        {
+          if (dump_file)
+            fprintf (dump_file,
+                     "  Using scales relying on static probabilities\n");
+          scales.safe_splice (estimated_scales);
+        }
+
        /* If we failed to find annotated entry or exit edge,
          look for exit edges and scale profile so the dest
          BB get all flow it needs.  This is imprecise because
-- 
2.34.1

Reply via email to