Re: [AutoFDO] prevent head_count of 0 with AFDO profile.

Kugan Vivekanandarajah <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <[email protected]>
Hi Honza,

Thanks for the review.

> On 4 Aug 2026, at 5:07 pm, Jan Hubička <[email protected]> wrote:
> 
> External email: Use caution opening links or attachments 
> Kugan,
> I apologize for late reaction. I finally got to reproduce this.
> I think we want to set the entry block count to
>  MAX (1, afdo_count_scale / 2)
> Receiving 0 samples on code that is known to be executed means that the real value should be somewhere between 0 and 1, so 1/2 is good estimate.  Setting count 1 will make all basic blocks to appear very hot and confuse the logic computing BB frequency.
> 
> Also I think you can do that just once in  afdo_calculate_branch_prob
> just after calling afdo_adjust_guessed_profile instead of doing it as a
> part of the fixup algorithm (which is already bit complicated).

Yes, this helps. I have added in one place as you suggested and this fixes the ICE.
If you don't have any objections, I will commit this patch. 

Thanks,
Kugan
0001-AutoFDO-Do-not-leave-entry-block-at-zero-after-guess.patch (application/octet-stream, 2 KB)
From 7a3629fa99cd68adb94cf0f48a15c2643f9bca50 Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <[email protected]>
Date: Sun, 17 May 2026 19:02:56 -0700
Subject: [PATCH] [AutoFDO] Do not leave entry block at zero after
 guessed-profile adjust

When afdo_adjust_guessed_profile runs scale_bbs, the entry block can be
scaled to 0 (auto FDO) while inner basic blocks keep nonzero AFDO
counts.

Example from profile annotation (stats.ii, _M_get_insert_unique_pos):
  ... total:264 head:-1
  bb 0 count updated 118111600 (estimated locally) -> 0 (auto FDO)
Example ICE at LTO (WPA merge of comdat clones):
  Merging profiles of _M_get_insert_unique_pos/10 count:64711123189
  (auto FDO) to _M_get_insert_unique_pos/1 count:0 (auto FDO)
  during IPA pass: modref
  lto1: internal compiler error: in apply_scale, at profile-count.h:1192
    ipa_merge_profiles (dstnum, dstden)

Fix by setting a zero entry block count to half a scaled sample after
afdo_adjust_guessed_profile.  This avoids zero-denominator scaling during
IPA profile merging without making nonzero basic blocks appear too hot.

gcc/ChangeLog:
  * auto-profile.cc (afdo_calculate_branch_prob): Set a zero entry
  block count to half a scaled sample after adjusting guessed profiles.

Regression tested on aarch64-linux-gnu with no new regressions

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/auto-profile.cc | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index f4562d7f2f9..440fbd152ed 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -4420,6 +4420,11 @@ afdo_calculate_branch_prob (bb_set *annotated_bb)
 	    }
       }
   afdo_adjust_guessed_profile (annotated_bb);
+  /* Avoid scaling with a zero entry count during IPA profile merging.  */
+  basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
+  if (!entry->count.nonzero_p ())
+    entry->count = profile_count::from_gcov_type
+      (MAX ((gcov_type) 1, autofdo::afdo_count_scale / 2)).afdo ();
   FOR_ALL_BB_FN (bb, cfun)
     {
       bb->aux = NULL;
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.