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

            Bug ID: 107163
           Summary: Compile time regression when using templated base
                    classes, virtual method, and Wall
           Product: gcc
           Version: 11.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cfsteefel at arista dot com
  Target Milestone: ---

When using templates to create a recursive base class structure, if the base
class uses a virtual method and -Wall is enabled, g++-11 shows much worse
compile time compared to g++-8.

Consider the following code:
# 0 "test.cpp"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "test.cpp"


class BaseType  {
   virtual int doSomething() {
      return 1;
   }
};

template< int Seq >
class DerivedType : public DerivedType< Seq - 1 > {
};

template<>
class DerivedType< -1 > : public BaseType {
};

int main() {
   auto * d = new DerivedType< COUNT >();
   delete d;
   return 0;
}

Compiled as:
g++-11 test.cpp -Wall -DCOUNT=200
g++-8 test.cpp -Wall -DCOUNT=200

In g++-11, with COUNT=100, the time to compile is around 2 seconds, while with
COUNT=200, the time is around 1 minute, and COUNT=400 took around 28 minutes.

For comparison, compiling the same code with g++-8 took around .3 seconds, 1.6
seconds, and 22 seconds, respectively.

Removing the virtual will lead to the compile time being near instant again, as
will removing the Wall flag (with g++-11).

Other details:
Compiler versions:
g++ (GCC) 11.3.1 20220421 (Red Hat 11.3.1-2)
g++ (GCC) 8.4.0 20200304 (Red Hat 8.4.0-4)
System:
Centos7 based linux
Compiler output:
test.cpp: In function 'int main()':
test.cpp:20:4: warning: deleting object of polymorphic class type
'DerivedType<100>' which has non-virtual destructor might cause undefined
behavior [-Wdelete-non-virtual-dtor]
   20 |    delete d;
      |    ^~~~~~~~
(fixing the warning changes nothing, and seemed to double the compile time).

extra g++-11/8 configuration options:
%{O*:%{!fomit-frame-pointer:-fno-omit-frame-pointer}} \
%{O*:%{!fno-wrapv:-fwrapv}} \
%{O*:%{!freorder-blocks-and-partition:-fno-reorder-blocks-and-partition}} \
%{mfpmath=sse:-msse2} \
%{!fsemantic-interposition:-fno-semantic-interposition} \
%{g:%{!g0:-gdwarf-4}} \


Using godbolt, I can see similar timing for g++-10 and g++-8, so this seems to
be a g++-11 specific regression.

Reply via email to