From 86ff4b7f8a53e32d737f78aca69a2ea6a6918dc3 Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
Date: Fri, 25 Jul 2025 03:36:30 -0700
Subject: [PATCH] [AutoFDO] Dont offline if inlined instance has 0 total count

annotate_cfg doesnt annotate it correctly in this case and causus ICE. There
is also no benefit in offlining this.

For:
_ZL27shift_amt_for_vec_perm_mask12machine_modeRK16vec_perm_indices9optab_tag total:0 head:-1

dump file: t.ii.091i.fnsummary
gcc/optabs.cc: In function 'rtx_def* shift_amt_for_vec_perm_mask(machine_mode, const vec_perm_indices&, optab)':
gcc/optabs.cc:7863:1: internal compiler error: in to_sreal_scale, at profile-count.cc:352
0x3ca209f internal_error(char const*, ...)
        ../../gcc/gcc/diagnostic-global-context.cc:517
0x3c5d5b3 fancy_abort(char const*, int, char const*)
        ../../gcc/gcc/diagnostic.cc:1818
0x1e42a3f profile_count::to_sreal_scale(profile_count, bool*) const
        ../../gcc/gcc/profile-count.cc:352
0x1b84933 analyze_function_body
        ../../gcc/gcc/ipa-fnsummary.cc:2941
0x1b86a13 compute_fn_summary(cgraph_node*, bool)
        ../../gcc/gcc/ipa-fnsummary.cc:3491
0x1b8ab4f inline_analyze_function(cgraph_node*)
        ../../gcc/gcc/ipa-fnsummary.cc:4630
0x1b8ad5f ipa_fn_summary_generate
        ../../gcc/gcc/ipa-fnsummary.cc:4673
0x1df23cf execute_ipa_summary_passes(ipa_opt_pass_d*)
        ../../gcc/gcc/passes.cc:2293
0x17b1e7f ipa_passes
        ../../gcc/gcc/cgraphunit.cc:2253
0x17b2287 symbol_table::compile()
        ../../gcc/gcc/cgraphunit.cc:2351
0x17b29fb symbol_table::finalize_compilation_unit()
        ../../gcc/gcc/cgraphunit.cc:2607

gcc/ChangeLog:

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

	* auto-profile.cc (function_instance::offline): Dont offline
 	if inlined instance has 0 total count

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

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index dcbc7c63af3..a26ac562777 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -1116,8 +1116,10 @@ function_instance::offline (function_instance *fn,
       fn->dump_inline_stack (dump_file);
       fprintf (dump_file, "\n");
     }
-  if (fn->total_count ())
-    fn->head_count_ = -1;
+  /* If fn does not have any samples, dont add it to afdo profile. */
+  if (!fn->total_count ())
+    return false;
+  fn->head_count_ = -1;
   afdo_source_profile->add_function_instance (fn);
   fn->set_in_worklist ();
   new_functions.safe_push (fn);
-- 
2.34.1

