force_nonzero () raises every count below profile_info->cutoff / 2 + 1 up
to that floor.  With -fauto-profile the cutoff is in the scale of the
sampled profile, which is unrelated to the scale of a local count.

Linking 827.cppcheck_s with -Ofast -flto -fauto-profile
-fprofile-partial-training:

/proj/grco/gcc/Linux_aarch64/upstream-main/20260916080922-6ef69b64/include/c++/17.0.0/bits/stl_vector.h:
 In member function '__dt_base ':
/proj/grco/gcc/Linux_aarch64/upstream-main/20260916080922-6ef69b64/include/c++/17.0.0/bits/stl_vector.h:842:7:
 error: caller edge count does not match BB count
  842 |       ~vector() _GLIBCXX_NOEXCEPT
      |       ^
edge count: 7704821123 (estimated locally)
 bb count: 14780070329 (estimated locally)
/proj/grco/gcc/Linux_aarch64/upstream-main/20260916080922-6ef69b64/include/c++/17.0.0/bits/stl_vector.h:842:7:
 error: caller edge count does not match BB count
edge count: 95496318925 (estimated locally)
 bb count: 183189496868 (estimated locally)
_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIS5_EED2Ev.lto_priv.0/24
 (__dt_base )
  Type: function definition analyzed
  Visibility: prevailing_def_ironly public visibility_specified 
visibility:hidden
  References:
  Referring: 
_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIS5_EED1Ev/64 
(alias)
  Read from file: /tmp/ccJhRA3s.ltrans0.o
  Availability: local
  Profile id: 1433291870
  Unit id: 2
  Function flags: count:27641799982 (estimated locally) first_run:247 body 
local merged_comdat
  Called by:
  Calls: _ZdlPvm/977 (7704821123 (estimated locally),0.28 per call) 
__builtin_unreachable/978 (0 (estimated locally),0.00 per call) _ZdlPvm/977 
(95496318925 (estimated locally),3.45 per call) __builtin_unreachable/978 (0 
(estimated locally),0.00 per call)
during IPA pass: inline
/proj/grco/gcc/Linux_aarch64/upstream-main/20260916080922-6ef69b64/include/c++/17.0.0/bits/stl_vector.h:842:7:
 internal compiler error: verify_cgraph_node failed
0x2549993 internal_error(char const*, ...)
        /var/jenkins/workspace/GCC_oss-main/gcc/diagnostic-global-context.cc:787
0x9d38c7 cgraph_node::verify_node()
        /var/jenkins/workspace/GCC_oss-main/gcc/cgraph.cc:4505
0x9bfc2f symtab_node::verify()
        /var/jenkins/workspace/GCC_oss-main/gcc/symtab.cc:1377
0x107fb23 optimize_inline_calls(tree_node*)
        /var/jenkins/workspace/GCC_oss-main/gcc/tree-inline.cc:5679
0xce9aeb inline_transform(cgraph_node*)
        /var/jenkins/workspace/GCC_oss-main/gcc/ipa-inline-transform.cc:871
0xe943a7 execute_one_ipa_transform_pass
        /var/jenkins/workspace/GCC_oss-main/gcc/passes.cc:2327
0xe943a7 execute_all_ipa_transforms(bool)
        /var/jenkins/workspace/GCC_oss-main/gcc/passes.cc:2390
0x9d980b cgraph_node::expand()
        /var/jenkins/workspace/GCC_oss-main/gcc/cgraphunit.cc:1866
0x9d980b cgraph_node::expand()
        /var/jenkins/workspace/GCC_oss-main/gcc/cgraphunit.cc:1826
0x9db8c3 expand_all_functions
        /var/jenkins/workspace/GCC_oss-main/gcc/cgraphunit.cc:2056
0x9db8c3 symbol_table::compile()
        /var/jenkins/workspace/GCC_oss-main/gcc/cgraphunit.cc:2434
0x9014b7 lto_main()
        /var/jenkins/workspace/GCC_oss-main/gcc/lto/lto.cc:722

Built and regression tested on aarch64-unknown-linux-gnui.

gcc/ChangeLog:

        * profile-count.cc (profile_count::force_nonzero): Do not raise
        local counts to the profile cutoff with -fauto-profile.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/profile-count.cc | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/gcc/profile-count.cc b/gcc/profile-count.cc
index 9be209efda2..f212b4be9e7 100644
--- a/gcc/profile-count.cc
+++ b/gcc/profile-count.cc
@@ -586,10 +586,16 @@ profile_count::force_nonzero () const
      Use cutoff value here to avoid situation where profile has large
      cutoff and we perform count = count * num / den where num is non-zero
      and den is 0.   If profile was scaled by large factor, forcing value
-     to 1 would lead to large scale factor.  */
-  gcov_unsigned_t small = profile_info ? profile_info->cutoff / 2 + 1
-                         : 1;
-  if (ret.m_val < small)
+     to 1 would lead to large scale factor.
+
+     With -fauto-profile the cutoff is in the scale of the sampled profile,
+     which is unrelated to the scale of a local count. Static
+     guess normalized to its own entry block.  Raising such a count to the
+     cutoff would scale it by an arbitrary factor, so compare only counts
+     that are in the profile's scale.  */
+  gcov_unsigned_t small = profile_info ? profile_info->cutoff / 2 + 1 : 1;
+  if (ret.m_val == 0
+      || ((ipa_p () || !flag_auto_profile) && ret.m_val < small))
     {
       ret.m_val = small;
       ret.m_quality = MIN (m_quality, ADJUSTED);
-- 
2.34.1

Reply via email to