From 95010bdc3c127cdb9cd562afa2543fb493650ca6 Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
Date: Thu, 4 Sep 2025 11:20:42 +1000
Subject: [PATCH 3/3] [AutoFDO] Fix profile consistency for self-recursive
 clones to fix ICE

Fix ICE "caller edge count does not match BB count"
with AutoFDO by improving profile handling when IPA-CP cannot fix up
incoming counts for self-recursive clones.

gcc/ChangeLog:

2025-09-02  Kugan Vivekanandarajah  <kvivekananda@nvidia.com>

	* ipa-cp.cc (update_counts_for_self_gen_clones): Enhanced
	profile quality handling when unable to fix up incoming counts.

Signed-off-by: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
---
 gcc/ipa-cp.cc | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index 21e2983743f..cbd909266f5 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -4730,9 +4730,18 @@ update_counts_for_self_gen_clones (cgraph_node *orig_node,
 	      adjust_clone_incoming_counts (n, &desc);
 	    }
 	  else if (dump_file)
-	    fprintf (dump_file,
-		     "       Unable to fix up incoming counts for %s.\n",
-		     n->dump_name ());
+	    {
+	      n->make_profile_local ();
+      	      if (n->count.quality () == AFDO)
+		n->make_profile_global0 (GUESSED_GLOBAL0_AFDO);
+     	      else
+		n->make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
+	      if (dump_file)
+		fprintf (dump_file,
+			 "       Unable to fix up incoming counts for %s, "
+			 "making profile quality to local.\n",
+			 n->dump_name ());
+	    }
 	}
     }
 
-- 
2.39.5 (Apple Git-154)

