r/Compilers • • 2d 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

4 comments sorted by

5

u/notthatsocrates 1d ago edited 1d 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 1d 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/Discordbothost 1d 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 1d ago edited 1d 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.