From 905eabb3a954f9d318e3512976d3ef5ad464fa3f Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
Date: Thu, 4 Sep 2025 11:12:27 +1000
Subject: [PATCH 1/3] [AutoFDO] Skip zero BB annotation for partial training to
 fix ICE

Fix internal compiler error when using -fprofile-partial-training with
AutoFDO by skipping annotation of basic blocks with zero counts to avoid
profile incompatibilities.

The ICE manifests as:
  gcc/optabs.cc: In function 'int shift_amt_for_vec_perm_mask()':
  gcc/optabs.cc:7863:1: internal compiler error: in to_sreal_scale, at profile-count.cc:352
  0x3d91ad3 internal_error(char const*, ...)
  	../../gcc/gcc/diagnostic-global-context.cc:786
  0x3da0e07 fancy_abort(char const*, int, char const*)
  	../../gcc/gcc/diagnostics/context.cc:1787
  0x1e8e5d7 profile_count::to_sreal_scale(profile_count, bool*) const
  	../../gcc/gcc/profile-count.cc:352
  0x1bceb43 analyze_function_body
  	../../gcc/gcc/ipa-fnsummary.cc:2941
  0x1bd0c23 compute_fn_summary(cgraph_node*, bool)
  	../../gcc/gcc/ipa-fnsummary.cc:3491

This occurs during IPA analysis when basic blocks have incompatible profile
qualities  due to forced zero annotations during
partial training, causing to_sreal_scale to fail when computing relative
scaling factors.

gcc/ChangeLog:
2025-09-02  Kugan Vivekanandarajah  <kvivekananda@nvidia.com>

	* auto-profile.cc (afdo_annotate_cfg): Skip zero BB annotation
	when profile not found and partial training is enabled.

Signed-off-by: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
---
 gcc/auto-profile.cc | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 7ff952632c3..5b6ca2f64cc 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -3878,19 +3878,22 @@ afdo_annotate_cfg (void)
   /* We try to preserve static profile for BBs with 0
      afdo samples, but if even static profile agrees with 0,
      consider it final so propagation works better.  */
-  for (basic_block bb : zero_bbs)
-    if (!bb->count.nonzero_p ())
-      {
-	update_count_by_afdo_count (&bb->count, 0);
-	set_bb_annotated (bb, &annotated_bb);
-	if (dump_file)
-	  {
-	    fprintf (dump_file, "  Annotating bb %i with count ", bb->index);
-	    bb->count.dump (dump_file);
-	    fprintf (dump_file,
-		     " (has 0 count in both static and afdo profile)\n");
-	  }
-      }
+  /* If !profile_found and flag_profile_partial_training
+     we will end up with incompatible profiles so leave it as local.  */
+  if (profile_found || !flag_profile_partial_training)
+    for (basic_block bb : zero_bbs)
+      if (!bb->count.nonzero_p ())
+	{
+	  update_count_by_afdo_count (&bb->count, 0);
+	  set_bb_annotated (bb, &annotated_bb);
+	  if (dump_file)
+	    {
+	      fprintf (dump_file, "  Annotating bb %i with count ", bb->index);
+	      bb->count.dump (dump_file);
+	      fprintf (dump_file,
+		       " (has 0 count in both static and afdo profile)\n");
+	    }
+	}
   /* Exit without clobbering static profile if there was no
      non-zero count.  */
   if (!profile_found)
-- 
2.39.5 (Apple Git-154)

