Do not subtract a lower quality clone count from an AutoFDO original.  The
clone count does not show that the samples belong to the clone.  Continue
subtracting counts based on feedback.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * cgraphclones.cc (count_left_in_original): New.
        (cgraph_node::create_clone): Use it when updating the original count.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/cgraphclones.cc | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/gcc/cgraphclones.cc b/gcc/cgraphclones.cc
index b5f2ce5ff2f..8f06683b343 100644
--- a/gcc/cgraphclones.cc
+++ b/gcc/cgraphclones.cc
@@ -91,6 +91,17 @@ along with GCC; see the file COPYING3.  If not see
 #include "symtab-thunks.h"
 #include "symtab-clones.h"
 
+/* Return the IPA count left after cloning.  Preserve an AutoFDO count when
+   COPY is lower quality because it cannot be subtracted reliably.  */
+
+static profile_count
+count_left_in_original (profile_count orig, profile_count copy)
+{
+  if (orig.quality () == AFDO && copy.quality () < AFDO)
+    return orig;
+  return orig - copy;
+}
+
 /* Create clone of edge in the node N represented by CALL_EXPR
    the callgraph.  */
 
@@ -419,18 +430,20 @@ cgraph_node::create_clone (tree new_decl, profile_count 
prof_count,
      preserve local profile.  */
   if (!new_inlined_to)
     prof_count = count.combine_with_ipa_count (prof_count);
+
   new_node->count = prof_count;
   new_node->has_omp_variant_constructs = this->has_omp_variant_constructs;
 
   /* Update IPA profile.  Local profiles need no updating in original.  */
   if (update_original)
     {
+      profile_count remainder
+       = count_left_in_original (count.ipa (), prof_count.ipa ());
       if (inlined_to)
-        count = count.combine_with_ipa_count_within (count.ipa ()
-                                                    - prof_count.ipa (),
+       count = count.combine_with_ipa_count_within (remainder,
                                                     inlined_to->count);
       else
-        count = count.combine_with_ipa_count (count.ipa () - prof_count.ipa 
());
+       count = count.combine_with_ipa_count (remainder);
     }
   new_node->decl = new_decl;
   new_node->order = order;
-- 
2.34.1

Reply via email to