Fix handle_missing_profiles to walk callees, update the callee, and
drop its profile only when the callee IPA count is known to be zero.
Include calls through aliases that resolve locally to the function.

Call handle_missing_profiles after applying an AutoFDO profile so calls
from sampled code can repair functions without matched samples.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * auto-profile.cc (auto_profile): Call handle_missing_profiles.
        * predict.cc (handle_missing_profiles): Fix callee traversal, alias
        handling, and zero count handling.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/auto-profile.cc |  3 +++
 gcc/predict.cc      | 50 ++++++++++++++++++++++++++++++---------------
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index eb7e6aa3f81..7fee25fd88a 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -4806,6 +4806,9 @@ auto_profile (void)
     pop_cfun ();
   }
 
+  /* Repair missing profiles from sampled calls.  */
+  handle_missing_profiles ();
+
   return 0;
 }
 } /* namespace autofdo.  */
diff --git a/gcc/predict.cc b/gcc/predict.cc
index 9bcecc02493..1e54d40cc22 100644
--- a/gcc/predict.cc
+++ b/gcc/predict.cc
@@ -3776,6 +3776,11 @@ drop_profile (struct cgraph_node *node, profile_count 
call_count)
 void
 handle_missing_profiles (void)
 {
+  struct missing_profile_calls
+  {
+    profile_count call_count;
+    gcov_type max_tp_first_run;
+  };
   const int unlikely_frac = param_unlikely_bb_count_fraction;
   struct cgraph_node *node;
   auto_vec<struct cgraph_node *, 64> worklist;
@@ -3784,26 +3789,32 @@ handle_missing_profiles (void)
      lost some profile.  Drop its function profile to PROFILE_GUESSED.  */
   FOR_EACH_DEFINED_FUNCTION (node)
     {
-      struct cgraph_edge *e;
-      profile_count call_count = profile_count::zero ();
-      gcov_type max_tp_first_run = 0;
+      missing_profile_calls d = { profile_count::zero (), 0 };
       struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
 
       if (node->count.ipa ().nonzero_p ())
         continue;
-      for (e = node->callers; e; e = e->next_caller)
-       if (e->count.ipa ().initialized_p () && e->count.ipa () > 0)
-         {
-            call_count = call_count + e->count.ipa ();
+      /* Include calls through aliases.  */
+      node->call_for_symbol_and_aliases (
+       [] (cgraph_node *alias, void *data)
+       {
+         missing_profile_calls *d = (missing_profile_calls *) data;
+         for (cgraph_edge *e = alias->callers; e; e = e->next_caller)
+           if (e->count.ipa ().initialized_p () && e->count.ipa () > 0)
+             {
+               d->call_count = d->call_count + e->count.ipa ();
 
-           if (e->caller->tp_first_run > max_tp_first_run)
-             max_tp_first_run = e->caller->tp_first_run;
-         }
+               if (e->caller->tp_first_run > d->max_tp_first_run)
+                 d->max_tp_first_run = e->caller->tp_first_run;
+             }
+         return false;
+       }, &d, false);
+      profile_count call_count = d.call_count;
 
       /* If time profile is missing, let assign the maximum that comes from
         caller functions.  */
-      if (!node->tp_first_run && max_tp_first_run)
-       node->tp_first_run = max_tp_first_run + 1;
+      if (!node->tp_first_run && d.max_tp_first_run)
+       node->tp_first_run = d.max_tp_first_run + 1;
 
       if (call_count > 0
           && fn && fn->cfg
@@ -3821,19 +3832,24 @@ handle_missing_profiles (void)
       struct cgraph_edge *e;
 
       node = worklist.pop ();
-      for (e = node->callees; e; e = e->next_caller)
+      for (e = node->callees; e; e = e->next_callee)
         {
-          struct cgraph_node *callee = e->callee;
+         enum availability availability;
+         struct cgraph_node *callee
+           = e->callee->ultimate_alias_target (&availability);
+         if (e->callee->alias && availability <= AVAIL_INTERPOSABLE)
+           continue;
           struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
 
-          if (!(e->count.ipa () == profile_count::zero ())
-             && callee->count.ipa ().nonzero_p ())
+         /* The edge count is local after dropping the caller profile.  */
+         profile_count callee_count = callee->count.ipa ();
+         if (!callee_count.initialized_p () || callee_count.nonzero_p ())
             continue;
           if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
              && fn && fn->cfg
               && profile_status_for_fn (fn) == PROFILE_READ)
             {
-              drop_profile (node, profile_count::zero ());
+             drop_profile (callee, profile_count::zero ());
               worklist.safe_push (callee);
             }
         }
-- 
2.34.1

Reply via email to