COMDAT copies receive the same AutoFDO samples, so summing their counts
during LTO multiplies the profile.  When merging copies of the same
symbol, keep the larger AutoFDO count instead.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * ipa-utils.cc (ipa_merge_profiles): Add SAME_SYMBOL.  Do not sum
        AutoFDO counts for copies of the same symbol.
        * ipa-utils.h (ipa_merge_profiles): Add SAME_SYMBOL.

gcc/lto/ChangeLog:

        * lto-symtab.cc (lto_symtab_merge_symbols_1): Pass SAME_SYMBOL.

gcc/testsuite/ChangeLog:

        * g++.dg/tree-prof/afdo-comdat-merge.C: New test.
        * g++.dg/tree-prof/afdo-comdat-merge-aux.cc: New file.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/ipa-utils.cc                              | 22 +++++++++---
 gcc/ipa-utils.h                               |  3 +-
 gcc/lto/lto-symtab.cc                         |  2 +-
 .../g++.dg/tree-prof/afdo-comdat-merge-aux.cc | 13 +++++++
 .../g++.dg/tree-prof/afdo-comdat-merge.C      | 35 +++++++++++++++++++
 5 files changed, 69 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge-aux.cc
 create mode 100644 gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge.C

diff --git a/gcc/ipa-utils.cc b/gcc/ipa-utils.cc
index ac052ad4b34..99fabfcd661 100644
--- a/gcc/ipa-utils.cc
+++ b/gcc/ipa-utils.cc
@@ -391,12 +391,14 @@ scale_ipa_profile_for_fn (struct cgraph_node *node, 
profile_count orig_count)
 
 /* SRC and DST are going to be merged.  Take SRC's profile and merge it into
    DST so it is not going to be lost.  Possibly destroy SRC's body on the way
-   unless PRESERVE_BODY is set.  */
+   unless PRESERVE_BODY is set.  SAME_SYMBOL is set if SRC and DST are
+   copies of the same symbol.  */
 
 void
 ipa_merge_profiles (struct cgraph_node *dst,
                    struct cgraph_node *src,
-                   bool preserve_body)
+                   bool preserve_body,
+                   bool same_symbol)
 {
   tree oldsrcdecl = src->decl;
   struct function *srccfun, *dstcfun;
@@ -430,9 +432,21 @@ ipa_merge_profiles (struct cgraph_node *dst,
 
   /* Either sum the profiles if both are IPA and not global0, or
      pick more informative one (that is nonzero IPA if other is
-     uninitialized, guessed or global0).   */
+     uninitialized, guessed or global0).  */
 
-  if ((dst->count.ipa ().nonzero_p ()
+  /* Same symbol AutoFDO counts describe the same executions.  Keep the
+     larger count.  */
+  if (same_symbol
+      && dst->count.ipa ().quality () == AFDO
+      && src->count.ipa ().quality () == AFDO)
+    {
+      if (dst->count.ipa () < src->count.ipa ())
+       {
+         copy_counts = true;
+         dst->count = src->count.ipa ();
+       }
+    }
+  else if ((dst->count.ipa ().nonzero_p ()
        || src->count.ipa ().nonzero_p ())
       && dst->count.ipa ().initialized_p ()
       && src->count.ipa ().initialized_p ())
diff --git a/gcc/ipa-utils.h b/gcc/ipa-utils.h
index d689d45c70c..07c35104d40 100644
--- a/gcc/ipa-utils.h
+++ b/gcc/ipa-utils.h
@@ -44,7 +44,8 @@ bool ipa_edge_within_scc (struct cgraph_edge *);
 int ipa_reverse_postorder (struct cgraph_node **);
 tree get_base_var (tree);
 void ipa_merge_profiles (struct cgraph_node *dst,
-                        struct cgraph_node *src, bool preserve_body = false);
+                        struct cgraph_node *src, bool preserve_body = false,
+                        bool same_symbol = false);
 bool recursive_call_p (tree, tree);
 bool stmt_may_terminate_function_p (function *fun, gimple *stmt, bool 
assume_return_or_eh);
 bitmap find_always_executed_bbs (function *fun, bool assume_return_or_eh);
diff --git a/gcc/lto/lto-symtab.cc b/gcc/lto/lto-symtab.cc
index 61b32dbc401..127a032303b 100644
--- a/gcc/lto/lto-symtab.cc
+++ b/gcc/lto/lto-symtab.cc
@@ -932,7 +932,7 @@ lto_symtab_merge_symbols_1 (symtab_node *prevailing)
         use prevailing definition.  */
       if (ce)
        ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
-                             dyn_cast<cgraph_node *> (e));
+                             dyn_cast<cgraph_node *> (e), false, true);
 
       /* If we decided to replace the node by TO, do it.  */
       if (e != to)
diff --git a/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge-aux.cc 
b/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge-aux.cc
new file mode 100644
index 00000000000..c2cbfe00790
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge-aux.cc
@@ -0,0 +1,13 @@
+inline int __attribute__ ((noinline)) work (int x)
+{
+  int s = 0;
+  for (int i = 0; i < 100; i++)
+    s += (x + i) * 3 + 1;
+  return s;
+}
+
+int __attribute__ ((noinline))
+f1 (int x)
+{
+  return work (x);
+}
diff --git a/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge.C 
b/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge.C
new file mode 100644
index 00000000000..f69e02c414d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-prof/afdo-comdat-merge.C
@@ -0,0 +1,35 @@
+// AutoFDO annotates each COMDAT copy of work from the same profile, so
+// merging the copies at link time must not sum their counts.
+// { dg-require-effective-target lto }
+// { dg-additional-sources "afdo-comdat-merge-aux.cc" }
+// { dg-options "-O2 -flto -fdump-ipa-cgraph" }
+// { dg-require-profiling "-fauto-profile" }
+
+volatile int sink;
+
+inline int __attribute__ ((noinline)) work (int x)
+{
+  int s = 0;
+  for (int i = 0; i < 100; i++)
+    s += (x + i) * 3 + 1;
+  return s;
+}
+
+extern int f1 (int);
+
+int __attribute__ ((noinline))
+f0 (int x)
+{
+  return work (x);
+}
+
+int
+main ()
+{
+  for (int i = 0; i < 1000000; i++)
+    sink = f0 (i) + f1 (i);
+  return 0;
+}
+
+// { dg-final-use-autofdo { scan-wpa-ipa-dump "Function flags: count:\[0-9\]+ 
\\(auto FDO\\)" "cgraph" } }
+// { dg-final-use-autofdo { scan-wpa-ipa-dump-not "Merging profiles of work/" 
"cgraph" } }
-- 
2.34.1

Reply via email to