r/Compilers • • 7d ago

Fast C++ Compiler Update

I shared some initial results for my C++ compiler a couple of weeks ago. I got a number of great questions, mostly around compiler quality. I have since added additional evidence to support quality and speedup claims:

  1. There are now extensive compliance tests for C++11, including the GCC and Clang C+11 compliance suites.
  2. I dramatically expanded the performance testing suite and it shows equivalent or better performance against GCC and Clang across a wide range of code shapes.
  3. I added a WebAssembly backend and put together a demo where you can compile c++ code with cppgm and Clang in the browser to see the speed difference yourself: https://incpption.com
  4. Cppgm can round-trip through LLVM (cppgm compiles LLVM and LLVM compiles cppgm). You can do this yourself in the browser and timing shows better performance at all optimization levels.
  5. I've put together a compile time analysis of the parts that cppgm does faster than Clang and GCC.

I hope this information is useful. Once again, I appreciate any thoughts or questions.

7 Upvotes

10 comments sorted by

5

u/notthatsocrates 7d ago edited 6d ago

I have built it locally and then I used it to recompile itself. It ran approx 4x faster than gcc/clang and the resulting compiler runs within 1% of the speed of the gcc/clang-built variants. This is pretty good evidence against the claim that its speed buys unusable code. It would require quite an effort to analyze it in depth but so far I think that you're in for some steep uphill climb with it. One more thing, I can't compile for my (preferred) ARM, so, in your opinion, how difficult is it to re-target it?

EDIT: At O3, compilation used about one fifth of GCC's CPU and one quarter of Clang's, excluding linking. Generated program run at parity (within 1%, small exceptions). The matrix multiplication was slower than clang's and on par with gcc.

2

u/vishvananda 7d ago

Hey, thanks for trying it out!

I managed to build the wasm backend by defining a new assignment and having an agent work on it for a couple of days. Getting a workable arm backend is probably a similar level of difficulty. I expect in would need some dedicated optimization work to get the perf quality up. Probably another few days of dedicated agent work.

2

u/vishvananda 5d ago

The 0.9.7 release now includes experimental arm support. Currently behind x86 in code gen perf at about 0.95x. I’ll optimize it soon

1

u/notthatsocrates 5d ago edited 5d ago

OK, I built it on ARM (M1 Max, running Linux). Against Clang 23.1.3, using the five-program ARM comparison suite:

Program            cppgm speed relative to Clang
---------------------------------------------------
Primes                  1.02× — effectively tied
---------------------------------------------------
Bubblesort    0.49× — roughly twice the CPU time
---------------------------------------------------
FloatMM                                    1.44×
---------------------------------------------------
Puzzle                                     1.52×
---------------------------------------------------
Treesort                                   0.95×

These ratios use process CPU time.

The interesting part came when I tried compiling the Apache stdcxx standard library, which I tinker with here. Building an alternative C++ standard library requires excluding the host compiler’s C++ headers. cppgm rejects -nostdinc++, so I used -nostdinc with explicit paths to the host C headers.

It compiled 96 of the 97 library source files on ARM. The remaining file, num_put.cpp, failed with:

ERROR: /.../stdcxx/src/fpclass.h:55:1: an emitted inline function that calls a reserved builtin this compiler lacks

The failing call comes from glibc’s math.h:1111. Its signbit macro selects __builtin_signbit for GCC ≥ 6 or Clang ≥ 3.3; otherwise, in C++ mode, it selects __builtin_signbitl.

cppgm’s ARM setup clears the host predefines, so neither compiler-version check succeeds. That selects __builtin_signbitl, which cppgm lacks, although it supports the generic __builtin_signbit.

This looks like a mismatch between the predefined environment supplied to glibc’s headers and cppgm’s builtin support. What GNU compatibility level is cppgm intended to advertise on ARM?

A conventional -dM -E macro dump would also make these integration issues easier to diagnose. Would you consider supporting it?

EDIT:

However, on x86_64 the library builds. The build with cppgm:

real	0m2.294s
user	0m1.928s
sys 	0m0.355s

Versus gcc:

real	0m12.655s
user	0m11.939s
sys 	0m0.661s

1

u/vishvananda 5d ago

Thanks for letting me know, I’ll add the missing define. Matching all of gcc and clangs defines in the stl is tricky so I occasionally run into things like this

1

u/vishvananda 4d ago

0.9.8 should have a fix for this. It also includes additional optimizations, although its still a bit behind x86 in codegen perf

1

u/notthatsocrates 4d ago

I tried it out, it fixes the compilation issue, but the problem has a larger scope. I filed a issue about the expectations of an independent C++ standard library and how they can be met. The investigation also pulled out some other config issues, more general in nature.

2

u/Discordbothost 7d ago

a side-by-side example of diagnostics for a broken template would help judge the day-to-day experience too. locating the first useful error, rather than chasing a cascade of follow-on errors, can matter as much as successful-build time.

1

u/vishvananda 7d ago edited 6d ago

I have not done a lot of work on improving warning and error diagnostics, so there is likely to be some room to improve here. To see what error messages we produce, you can look at the student export of each tests that expects to fail, for example:
https://github.com/vishvananda/cppgm-assignments/blob/main/pa14/tests/spec/100-unused-template-sizeof-subscript-bad.ref.stdout

In general i think cppgm is behind here and could use some work. Here are a couple egregious ones:

1. An undeclared name in a template

pa14/tests/general/100-indirect-dependent-base-unqualified-bad.t

template<class T> struct Middle : T {};
template<class T> struct Derived : Middle<T> {
void use() { Unknown = 1; }
};
int main() { return 0; }

cppgm 3:16: undeclared name
gcc In member function 'void Derived<T>::use()':
3:16: error: 'Unknown' was not declared in this scope [-Wtemplate-body]
clang 3:16: error: use of undeclared identifier 'Unknown'

All three point at the same spot, but only GCC and Clang say which name is undeclared.

2. Missing typename before a dependent type

pa14/tests/general/300-dependent-parameter-type-needs-typename-bad.t

template<class T> struct Reader { void read(const T::Metadata::element& value); };

cppgm 3:71: syntax error
gcc 3:51: error: need 'typename' before 'T::Metadata::element' because 'T::Metadata' is a dependent scope [-Wtemplate-body]
clang 3:51: error: missing 'typename' prior to dependent type name 'T::Metadata::element' is a C++20 extension [-Werror,-Wc++20-extensions]

cppgm reports where the parse broke down (column 71, at value), not the cause. GCC and Clang name the missing typename at column 51. Clang rejects it here only because of -pedantic-errors; normally it warns that C++20 allows this form.

But we do have a few cases where we do better than the otehrs. For example:

1. A qualified injected-class-name used as a type

pa14/tests/general/100-qualified-injected-constructor-name-bad.t

struct K { int x; };
template<class T> struct A { T a; };
int main() { return int(sizeof(K::K*)) + int(sizeof(A<int>::A*)); }

cppgm 5:32: a class's qualified injected-class-name names its constructor, not a type
gcc 5:37: error: expected primary-expression before ')' token
5:63: error: expected primary-expression before ')' token
clang 5:35: error: qualified reference to 'K' is a constructor name rather than a type in this context
5:61: error: qualified reference to 'A' is a constructor name rather than a type in this context

cppgm and Clang both state the rule: K::K names the constructor, not the class. GCC only reports a stray ). Clang also reports the second violation, A<int>::A; cppgm stops at the first.

2. typename on an unqualified name

pa14/tests/general/100-typename-unqualified-name-bad.t

template <class T> struct S { typename T x; };

cppgm 3:40: typename applied to an unqualified name
gcc 3:40: error: expected nested-name-specifier before 'T'
3:40: error: expected ';' at end of member declaration
3:40: error: declaration of 'int S<T>::T' shadows template parameter
3:11: note: template parameter 'T' declared here
3:42: error: 'x' does not name a type
clang 3:40: error: expected a qualified name after 'typename'

cppgm and Clang give one accurate line each. GCC's error recovery produces four errors, including a misleading claim that the code redeclares T.

1

u/notthatsocrates 3d ago

Have you thought about enabling Discussions in your cppgm repo? And, have you done any analysis on the source of the speed-up in your implementation (apologies if I missed that)?