[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-10-05 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

Sergei Trofimovich  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |FIXED
   Assignee|unassigned at gcc dot gnu.org  |slyfox at gcc dot 
gnu.org

--- Comment #10 from Sergei Trofimovich  ---
Fixed in master.

Thanks for the report!

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-10-05 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #9 from CVS Commits  ---
The master branch has been updated by Sergei Trofimovich :

https://gcc.gnu.org/g:043a6fcbc27f8721301eb2f72a7839f54f393003

commit r14-4421-g043a6fcbc27f8721301eb2f72a7839f54f393003
Author: Sergei Trofimovich 
Date:   Wed Sep 27 14:29:12 2023 +0100

ipa-utils: avoid uninitialized probabilities on ICF [PR111559]

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 reported by IPA profile:

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

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

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

PR ipa/111283
PR gcov-profile/111559

gcc/
* ipa-utils.cc (ipa_merge_profiles): Avoid producing
uninitialized probabilities when merging counters with zero
denominators.

gcc/testsuite/
* gcc.dg/tree-prof/pr111559.c: New test.

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-28 Thread sirl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #8 from Franz Sirl  ---
The proposed patch on top of r14-4307 fixes the profiled bootstrap here.

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-27 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #7 from Sergei Trofimovich  ---
Proposed conservative fix as
https://gcc.gnu.org/pipermail/gcc-patches/2023-September/631526.html

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-27 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #6 from Sergei Trofimovich  ---
Uninitialized value comes from `ipa_merge_profiles()` for our `rule1_same()`
alias and `rule1()` functions:

  // in gcc/ipa-icf.cc:

  else if (create_alias)
{
  alias->icf_merged = true;

  /* Remove the function's body.  */
  ipa_merge_profiles (original, alias);
  // ...

If I comment out `ipa_merge_profiles (original, alias);` call to leave
`original` as is then failure does not happen. Which means at least
`original`'s profile is fine.

Tracing through `ipa_merge_profiles()` we generate uninitialized probalility
profile when divide by zero at:

  // in gcc/ipa-utils.cc

void
ipa_merge_profiles (struct cgraph_node *dst,
struct cgraph_node *src,
bool preserve_body)
// ...

  /* TODO: merge also statement histograms.  */
  FOR_ALL_BB_FN (srcbb, srccfun)
{
  unsigned int i;

  if (copy_counts) { /* snip: ireelevant */ }
  else
{
  for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
{
  edge srce = EDGE_SUCC (srcbb, i);
  edge dste = EDGE_SUCC (dstbb, i);
  dste->probability =
dste->probability * dstbb->count.ipa ().probability_in
 (dstbb->count.ipa ()
  + srccount.ipa ())
+ srce->probability * srcbb->count.ipa ().probability_in
 (dstbb->count.ipa ()
  + srccount.ipa ());
}
  dstbb->count = dstbb->count.ipa () + srccount.ipa ();
}
}
// ...

Here `dstbb->count.ipa () + srccount.ipa ()` is zero.

This assert should expose it as well:

--- a/gcc/ipa-utils.cc
+++ b/gcc/ipa-utils.cc
@@ -651,13 +651,15 @@ ipa_merge_profiles (struct cgraph_node *dst,
{
  edge srce = EDGE_SUCC (srcbb, i);
  edge dste = EDGE_SUCC (dstbb, i);
+
+ profile_count den = dstbb->count.ipa () + srccount.ipa ();
+ gcc_assert(den.nonzero_p());
+
  dste->probability =
dste->probability * dstbb->count.ipa ().probability_in
-(dstbb->count.ipa ()
- + srccount.ipa ())
+(den)
+ srce->probability * srcbb->count.ipa ().probability_in
-(dstbb->count.ipa ()
- + srccount.ipa ());
+(den);
}
  dstbb->count = dstbb->count.ipa () + srccount.ipa ();
}

If we attach `gdb` it agrees we exercise these edges 0 times.

(gdb) call dstbb->count.debug()
0 (precise)
(gdb) call srccount.ipa ().debug()
0 (precise)

For comparison we are trying to clobber `always` probability with `undefined`:

(gdb) call dste->probability.debug()
always

What edge is that?

(gdb) call debug_edge(srce)
edge (bb_3, bb_4)

__attribute__((noinline))
void rule1 ()
{
  int p.0_1;

   [count: 2]:
  p.0_1 = p;
  if (p.0_1 != 0)
goto ; [0.00%]
  else
goto ; [100.00%]

   [count: 0]:
  edge ();

   [count: 2]:
  return;

}

`always` should valid for `bb_3->bb_4`. But for our data input it's `never`.

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-27 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #5 from Sergei Trofimovich  ---
Slightly shorter example that does not rely on inline:

// $ cat bug.c
__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();
}



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

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-27 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

--- Comment #4 from Sergei Trofimovich  ---
Looks like identical code folding creates uninitialized profile counters if
there are any edges in folded functions.

I think cvise did a decent job extracting the reproducer below. Here is a
single-file trigger on `--enable-checking=yes` `gcc` from `master`:

```
// $ cat bug.c
__attribute__((noipa)) static void edge(void) {}

static void rule1(int *p) {
edge();
if (*p) edge();
}

static void rule1_same(int *p) {
edge();
if (*p) edge();
}

__attribute__((noipa)) int main(void) {
int p = 0;
rule1();
rule1_same();
}
```

Trigger:

```
$ echo PG
$ gcc -O2 -fprofile-generate bug.c -o b -fopt-info
$ echo RUN
$ ./b
$ echo PU
$ gcc -O2 -fprofile-use -fprofile-correction bug.c -o b -fopt-info
```

Running:

```
PG
$ gcc -O2 -fprofile-generate bug.c -o b -fopt-info
bug.c:15:5: optimized:  Inlined rule1.constprop/28 into main/3 which now has
time 75.28 and size 51, net change of -6.
bug.c:16:5: optimized:  Inlined rule1_same.constprop/27 into main/3 which now
has time 94.56 and size 72, net change of -6.

RUN
$ ./b

PU
$ gcc -O2 -fprofile-use -fprofile-correction bug.c -o b -fopt-info
bug.c:3:13: optimized: Semantic equality hit:rule1/1->rule1_same/2
bug.c:3:13: optimized: Assembler symbol names:rule1/1->rule1_same/2
bug.c:15:5: optimized:  Inlined rule1.constprop/5 into main/3 which now has
time 26.00 and size 10, net change of +2.
bug.c:16:5: optimized:  Inlined rule1.constprop/4 into main/3 which now has
time 27.00 and size 12, net change of -6.

bug.c: In function 'main':
bug.c:13:28: error: probability of edge 3->4 not initialized
   13 | __attribute__((noipa)) int main(void) {
  |^~~~
bug.c:13:28: error: probability of edge 5->6 not initialized
during IPA pass: inline
bug.c:13:28: internal compiler error: verify_flow_info failed
```

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-24 Thread sirl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

Franz Sirl  changed:

   What|Removed |Added

 CC||sirl at gcc dot gnu.org

--- Comment #3 from Franz Sirl  ---
Looks like a dup of the profiledbootstrap fail bug 111283

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-24 Thread slyfox at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

Sergei Trofimovich  changed:

   What|Removed |Added

 CC||slyfox at gcc dot gnu.org

--- Comment #2 from Sergei Trofimovich  ---
Possibly exposed by r14-3459-g0c78240fd7d519 "Check that passes do not forget
to define profile"

[Bug gcov-profile/111559] [14 regression] ICE when building Python with PGO

2023-09-23 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111559

Andrew Pinski  changed:

   What|Removed |Added

 CC||hubicka at gcc dot gnu.org
   Target Milestone|--- |14.0