[gcc r17-3483] [AutoFDO] Do not leave entry block at zero after guessed-profile adjust

Kugan Vivekanandarajah via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:ab0712667b0461e34679cade62369a2c92fbc9be

commit r17-3483-gab0712667b0461e34679cade62369a2c92fbc9be
Author: Kugan Vivekanandarajah <[email protected]>
Date:   Fri Aug 21 10:57:09 2026 +1000

    [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 scaled sample after
    afdo_adjust_guessed_profile.
    
    gcc/ChangeLog:
    
    2026-08-21  Kugan Vivekanandarajah  <[email protected]>
    
            * auto-profile.cc (afdo_calculate_branch_prob): Set a zero entry
            block count to half a scaled sample after adjusting guessed profiles.

Diff:
---
 gcc/auto-profile.cc | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 324a26cefc8c..643e0622df5b 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -4421,6 +4421,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;
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.