From 5848d133fa456f39a26878444660dd7f626723c9 Mon Sep 17 00:00:00 2001
From: Kugan <kvivekananda@nvidia.com>
Date: Mon, 21 Jul 2025 21:37:57 -0700
Subject: [PATCH] [AutoFDO] Propagate single succ/pred block's annotation

In afdo_propagate_edge, annotate edge from the connected
basic_block if they have single predcessor/succcessor.

Tested with spec2017 intrate/intspeed and shows slight improvement.

gcc/ChangeLog:

2025-07-21  Kugan Vivekanandarajah  <kvivekananda@nvidia.com>

	* auto-profile.cc (afdo_propagate_edge): Propagate single
 	succ or pred block's annotation.
---
 gcc/auto-profile.cc | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index d1954b4fad6..dcbc7c63af3 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -3179,7 +3179,7 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
 	bb->count = total_known_count;
 	changed = true;
       }
-    else if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb))
+    if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb))
       {
 	if (bb->count > total_known_count)
 	  {
@@ -3242,8 +3242,33 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
 	    changed = true;
 	  }
       }
-  }
-  return changed;
+    else
+      {
+	FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
+	  {
+	    basic_block succ_or_pred_bb = is_succ ? e->dest : e->src;
+	    /* Propagate single succ or pred block's annotation.  */
+	    if ((is_succ ? single_succ_p (succ_or_pred_bb) : single_pred_p (succ_or_pred_bb))
+		&& !AFDO_EINFO (e)->is_annotated ()
+		&& is_bb_annotated (succ_or_pred_bb, *annotated_bb))
+	      {
+		if (dump_file)
+		  {
+		    fprintf (dump_file, "  Annotating edge %i->%i count from ",
+			     e->src->index, e->dest->index);
+		    fprintf (dump_file, "  Annotating bb %i count from ",
+					      succ_or_pred_bb->index);
+		    succ_or_pred_bb->count.dump (dump_file);
+		    fprintf (dump_file, "\n");
+		  }
+		AFDO_EINFO (e)->set_count (succ_or_pred_bb->count);
+		AFDO_EINFO (e)->set_annotated ();
+		changed = true;
+	      }
+	  }
+      }
+   }
+   return changed;
 }
 
 /* Special propagation for circuit expressions. Because GCC translates
-- 
2.34.1

