https://gcc.gnu.org/g:f0b3c57ea6b87f51eb0a9bfd7c4438625cb7ed17

commit r16-1773-gf0b3c57ea6b87f51eb0a9bfd7c4438625cb7ed17
Author: Jan Hubicka <hubi...@ucw.cz>
Date:   Mon Jun 30 09:14:46 2025 +0200

    Re-add logic to mitigate some afdo profile inconsistencies
    
    This patch re-adds logic to increase counts of annotated basic blocks if 
otherwise
    the Kirhoff law can not be solved.  This is done only in easy cases where 
total
    count of in or out edges is smaller than the count of BB or when BB has 
single
    exit which is annotated by small count.
    
    This helps to solve problems seen i.e. in parest where header of loops gets 
too
    low count because vectorizer replaced the IV condiitonal and did not 
preserved
    debug info.  We should solve the debug info issues as well, and simiar 
problems
    can now be tracked by in afdo debug dumps.
    
    gcc/ChangeLog:
    
            * auto-profile.cc 
(autofdo_source_profile::offline_external_functions):
            Add missing newline in dump.
            (afdo_propagate_edge): If annotated BB or edge has too small count
            bump it up to mitigate profile imprecisions caused by vectorizer.
            (afdo_propagate): Increase number of iteraitons and fix dump

Diff:
---
 gcc/auto-profile.cc | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 44e7faa8fee6..d78f2cb42b5c 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -1308,7 +1308,7 @@ autofdo_source_profile::offline_external_functions ()
              if (dump_file)
                fprintf (dump_file,
                         "string table in auto-profile contains"
-                        " duplicated name %s", n1);
+                        " duplicated name %s\n", n1);
              to_symbol_name.put (i, index);
            }
          continue;
@@ -2491,6 +2491,21 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
        set_bb_annotated (bb, annotated_bb);
        changed = true;
       }
+    else if (is_bb_annotated (bb, *annotated_bb)
+            && bb->count < total_known_count)
+      {
+       if (dump_file)
+         {
+           fprintf (dump_file, "  Increasing bb %i count from ",
+                    bb->index);
+           bb->count.dump (dump_file);
+           fprintf (dump_file, " to ");
+           total_known_count.dump (dump_file);
+           fprintf (dump_file, " hoping to mitigate afdo inconsistency\n");
+         }
+       bb->count = total_known_count;
+       changed = true;
+      }
     else if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb))
       {
        if (bb->count > total_known_count)
@@ -2533,6 +2548,27 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
              }
          }
       }
+    else if (num_unknown_edges == 0
+            && is_bb_annotated (bb, *annotated_bb)
+            && (is_succ ? single_succ_p (bb) : single_pred_p (bb)))
+      {
+       edge e = is_succ ? single_succ_edge (bb) : single_pred_edge (bb);
+       if (AFDO_EINFO (e)->is_annotated ()
+           && AFDO_EINFO (e)->get_count () < bb->count)
+         {
+           if (dump_file)
+             {
+               fprintf (dump_file, "  Increasing edge %i->%i count from ",
+                        e->src->index, e->dest->index);
+               AFDO_EINFO (e)->get_count ().dump (dump_file);
+               fprintf (dump_file, " to ");
+               bb->count.dump (dump_file);
+               fprintf (dump_file, " hoping to mitigate afdo inconsistency\n");
+             }
+           AFDO_EINFO (e)->set_count (bb->count);
+           changed = true;
+         }
+      }
   }
   return changed;
 }
@@ -2662,10 +2698,11 @@ afdo_propagate (bb_set *annotated_bb)
                     ((basic_block)bb->aux)->index,
                     bb->index);
            bb->count.dump (dump_file);
+           fprintf (dump_file, "\n");
          }
       }
 
-  while (changed && i++ < 10)
+  while (changed && i++ < 100)
     {
       changed = false;
 
@@ -2676,7 +2713,7 @@ afdo_propagate (bb_set *annotated_bb)
       afdo_propagate_circuit (*annotated_bb);
     }
   if (dump_file)
-    fprintf (dump_file, "Propated in %i iterations %s\n",
+    fprintf (dump_file, "Propagation took %i iterations %s\n",
             i, changed ? "; iteration limit reached\n" : "");
 }

Reply via email to