public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug ipa/101839] New: [9/10/11/12 Regression] Hand in C++ code with -fdevirtualize
@ 2021-08-10  7:02 marxin at gcc dot gnu.org
  2021-08-10  7:04 ` [Bug ipa/101839] " marxin at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-08-10  7:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101839

            Bug ID: 101839
           Summary: [9/10/11/12 Regression] Hand in C++ code with
                    -fdevirtualize
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
                CC: hubicka at gcc dot gnu.org, marxin at gcc dot gnu.org
  Target Milestone: ---

Reported by leon zadorin <leonleon77@gmail.com> via email:

The following produces hanged application (busy-polling by what appears to be
calling self-method in a loop indefinitely) when, essentially, "-fdevirtualize"
is brought into play (albeit with other options possibly).

The reproduction was achieved on
Linux: gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2), x86_64-linux-gnu
and Windows: gcc version 11.1.0 (GCC), x86_64-w64-mingw32

Code compiled as:
c++ -O3 -fprofile-generate ./devirtualize.cpp

Whilst consistently reproducible ("done writing" is never printed out), the
buggy behavior is rather strange and any of the following would change/"fix"
the situation:

Taking out the anonymous namespace;
Marking BA::type() as non-final;
Taking out std::map from CA;
Adding -fno-devirtualize flag to GCC compilation flags.

Note: taking out -fprofile-generate from GCC compilation flags results in
different busy-print loop (potentially followed by segfault) i.e. different,
yet still buggy behaviour.

The repro code could be simplified even further (e.g. by taking out Buf class,
reducing number of calls to 'add', etc) and  still reproduce in gcc-9.2 on
Linux. However on gcc-11.1 on Windows it would then no longer reproduce.
So I've left the following code in its current state so as to hopefully induce
more consistent reproduction across different versions.

Here it is, devirtualize.cpp (also attached):

#include <string.h>
#include <iostream>
#include <map>
namespace {
  struct Buf {
    char * buf; int a{0}; int b{0};
    Buf(char * b) : buf(b) { }
    void add(int v) {
      ::memcpy(buf, &v, sizeof(v));
      a += sizeof(v);
      b += sizeof(v);
    }
  };
  struct A {
    virtual void fill(Buf &buf) {
      buf.add(type());
      buf.add(type());
    }
    virtual ~A() {}
    virtual int type() = 0;
  };
  struct BA : A {
    void fill(Buf &buf) {
      A::fill(buf);
      buf.add(type());
      buf.add(type());
    }
    int type() final {
      return 1;
    }
  };
  struct CBA final : BA {
  };
  struct CA final : A {
    ::std::map<int, int> m;
    int type() final {
      return 2;
    }
  };
}
int main(int argc, char ** ) {
  char d[1024];
  CBA cba;
  ::std::cerr << "writing \n"; ::std::cerr.flush();
  Buf buf(d);
  cba.fill(buf);
  ::std::cerr << "done writing \n"; ::std::cerr.flush();
  CA ca;
}

Richi replied:

The issue also reproduces with GCC 7.5
(on Leap 15.3), -O -fprofile-generate -fdevirtualize is enough
to trigger it there.  Confirmed also with GCC 9, 10, 11 and trunk
and those options.

We hang in

Program received signal SIGINT, Interrupt.
0x00000000004017d5 in (anonymous namespace)::A::fill (buf=..., 
    this=<optimized out>) at t.C:16
16            buf.add(type());
Missing separate debuginfos, use: zypper install 
libgcc_s1-debuginfo-10.3.0+git1587-1.6.4.x86_64 
libstdc++6-debuginfo-10.3.0+git1587-1.6.4.x86_64
(gdb) bt
#0  0x00000000004017d5 in (anonymous namespace)::A::fill (buf=..., 
    this=<optimized out>) at t.C:16
#1  (anonymous namespace)::BA::fill (this=this@entry=0x7fffffffde08, 
buf=...)
    at t.C:24
#2  0x0000000000401860 in main (argc=<optimized out>) at t.C:46
(gdb) s        
(anonymous namespace)::BA::fill (this=this@entry=0x7fffffffde08, buf=...)
    at t.C:24
24            A::fill(buf);
(gdb) s
(anonymous namespace)::A::fill (buf=..., this=0x7fffffffddd8) at t.C:15
15          virtual void fill(Buf &buf) {
(gdb) s
16            buf.add(type());
(gdb) s
(anonymous namespace)::BA::fill (this=this@entry=0x7fffffffde08, buf=...)
    at t.C:24
24            A::fill(buf);
(gdb) s
(anonymous namespace)::A::fill (buf=..., this=0x7fffffffddd8) at t.C:15
15          virtual void fill(Buf &buf) {
(gdb) s
16            buf.add(type());


there is one thing that I'm not sure is allowed.  You do:

  struct A {
    virtual void fill(Buf &buf) {
      buf.add(type());
      buf.add(type());
    }
    virtual ~A() {}
    virtual int type() = 0;
  };
  struct BA : A {
    void fill(Buf &buf) {
      A::fill(buf);
      buf.add(type());
      buf.add(type());
    }

thus override virtual A::fill with non-virtual BA::fill.  But this
might be where the devirtualization code (or the indirect call/devirt
profiling code?) is confused.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-07-07 10:40 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10  7:02 [Bug ipa/101839] New: [9/10/11/12 Regression] Hand in C++ code with -fdevirtualize marxin at gcc dot gnu.org
2021-08-10  7:04 ` [Bug ipa/101839] " marxin at gcc dot gnu.org
2021-08-10  7:09 ` marxin at gcc dot gnu.org
2021-08-10  7:13 ` [Bug ipa/101839] [9/10/11/12 Regression] Hang " rguenth at gcc dot gnu.org
2021-08-10  7:33 ` redi at gcc dot gnu.org
2022-05-27  9:45 ` [Bug ipa/101839] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:45 ` jakub at gcc dot gnu.org
2022-07-29 12:24 ` rguenth at gcc dot gnu.org
2022-08-10  3:53 ` yinyuefengyi at gmail dot com
2022-08-10 13:19 ` hubicka at gcc dot gnu.org
2022-08-10 13:44 ` hubicka at gcc dot gnu.org
2022-08-12 14:27 ` hubicka at gcc dot gnu.org
2022-08-15 13:57 ` [Bug ipa/101839] [10/11/12 " marxin at gcc dot gnu.org
2023-04-27 13:21 ` [Bug ipa/101839] [10/11 " rguenth at gcc dot gnu.org
2023-07-07 10:40 ` [Bug ipa/101839] [11 " rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).