ipa-utils: avoid generating uninitialized probabilities on merges.

Message ID 20230927144438.3337173-1-slyich@gmail.com
State Accepted
Headers
Series ipa-utils: avoid generating uninitialized probabilities on merges. |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Sergei Trofimovich Sept. 27, 2023, 2:44 p.m. UTC
  From: Sergei Trofimovich <siarheit@google.com>

r14-3459-g0c78240fd7d519 "Check that passes do not forget to define profile"
exposed check failures in cases when gcc produces uninitialized profile
probabilities. In case of PR/111559 uninitialized profile is generated
by edges executed 0 times during profile:

    __attribute__((noipa)) static void edge(void) {}

    int p = 0;

    __attribute__((noinline))
    static void rule1(void) { if (p) edge(); }

    __attribute__((noinline))
    static void rule1_same(void) { if (p) edge(); }

    __attribute__((noipa)) int main(void) {
        rule1();
        rule1_same();
    }

    $ gcc -O2 -fprofile-generate bug.c -o b -fopt-info
    $ ./b
    $ gcc -O2 -fprofile-use -fprofile-correction bug.c -o b -fopt-info

    bug.c: In function 'rule1':
    bug.c:6:13: error: probability of edge 3->4 not initialized
        6 | static void rule1(void) { if (p) edge(); }
          |             ^~~~~
    during GIMPLE pass: fixup_cfg
    bug.c:6:13: internal compiler error: verify_flow_info failed

The change conservatively ignores updates with uninitialized values and
uses initially assigned probabilities (`always` probability in case of
the example).

gcc/
	PR/111283
	PR/111559
	* ipa-utils.cc (ipa_merge_profiles): Avoid producing
	  uninitialized probabilities when merging counters with zero
	  denominators.
---
 gcc/ipa-utils.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gcc/ipa-utils.cc b/gcc/ipa-utils.cc
index 956c6294fd7..7c53ae9dd45 100644
--- a/gcc/ipa-utils.cc
+++ b/gcc/ipa-utils.cc
@@ -651,13 +651,17 @@  ipa_merge_profiles (struct cgraph_node *dst,
 		{
 		  edge srce = EDGE_SUCC (srcbb, i);
 		  edge dste = EDGE_SUCC (dstbb, i);
-		  dste->probability = 
+		  profile_probability merged =
 		    dste->probability * dstbb->count.ipa ().probability_in
 						 (dstbb->count.ipa ()
 						  + srccount.ipa ())
 		    + srce->probability * srcbb->count.ipa ().probability_in
 						 (dstbb->count.ipa ()
 						  + srccount.ipa ());
+		  /* We produce uninitialized probabilities when
+		     denominator is zero: https://gcc.gnu.org/PR111559.  */
+		  if (merged.initialized_p ())
+		    dste->probability = merged;
 		}
 	      dstbb->count = dstbb->count.ipa () + srccount.ipa ();
 	    }