Compiler Explorer is an extremely useful interactive web-site created by Matt Godbolt. You can paste in any C or C++ source code (or one of the other 19 supported languages), select a compiler from a long list of choices, run it and study the generated assembly code. Over the years, Matt has added a large number of useful features that make it stand out. That’s why Solid Sands is proud to be one of Compiler Explorer’s corporate sponsors, and why we use it every day.

Our basic mode of interaction consists of pasting a corner case test on the left and viewing the compiler output on the right. It’s that easy. Sometimes the compiler disagrees with the expected result of the test, which implies that a more detailed review of the language definition is required. Important tip: don’t trust the compiler to throw light on the language standard for you.

Matt explains that Compiler Explorer started from the need to upgrade from C++98 to C++0x without incurring a performance hit. The first version looked like this:

watch ‘gcc -S -o – /tmp/test.cc | c++filt | grep -v “^\s+.”‘

and it turned out to be super helpful to monitor the effect of using different C++ constructs.

Compiler Explorer debuted in 2012, when it was still called GCC Explorer. Today it supports close to 600 (!) compilers. Its servers perform about 7 million compilations every month for users all over the world. The vast majority are for C++ followed by C, with the top compilers selected being the GCC compiler followed by Clang.

One advantage of Compiler Explorer is that it can act like a back-in-time machine, allowing software developers to easily revisit compilers that have long since disappeared from their development tool chain. For example, Compiler Explorer includes the GCC 4.8.5 compiler. It is just five years old but already considered ancient. You can’t install it today without fixing package dependences. For infrastructure projects with software support lifetimes that run into tens of years, this feature of Compiler Explore can prove very useful.

When Matt saw some of the compiler bugs discovered by our compiler test and validation suite for C and C++, SuperTest, his immediate comment was: “Hey, what a scary world your engineers live in!”. Making that world a less scary place is why we created SuperTest. We want developers to be able to use compilers for mission-critical applications and sleep well at night.

That leads us on to another great feature of Compiler Explorer: sharing configurations with short links. Here are a few examples of typical compiler bugs:

Example 1

https://godbolt.org/z/V_R83W is an example of a delicate optimization error in that old GCC 4.8.5 compiler. A loop optimization creates a race condition in parallel code by caching the (OpenMP) shared variable ‘globFound’. Running the program, frequently (non-deterministically) returns the wrong value of 255 instead of 9. (Use the ‘rerun’ icon enough times and you will see the output change). This bug was present in all versions of GCC-4 and was fixed in GCC-5. Running parallel threads is one of Compiler Explorer’s recent new features.

Example 2

https://godbolt.org/z/dkpK3q is a bug in a more modern compiler, Clang 10.0.0. This compiler has some issues handling partial specialization in variable templates. Interestingly, you get the correct value if an intermediate use of that variable template is made. The problem is related to DR-1711 in C++14.

Example 3

This bug is in MSVC: https://cpp.godbolt.org/z/d44yVe. Also marvel at the efficient assembly code generated by CLANG and GCC at high optimization levels.

Matt is proud of the fact that Compiler Explorer is open source – maintained by a core team and around a dozen occasional contributors. All the code, including the compiler build system and the infrastructure configuration, can be found at https://github.com/compiler-explorer/, allowing you to install it at home or on a private server. As part of our Continuous Integration environment at Solid Sands, we run tests on a large number of compilers. We use a cloned implementation of Compiler Explorer to speed things up without hammering Matt’s servers. Compiler Explorer has an API, so you don’t need to go through the GUI to use it.

Compiler Explorer is supported by community contributions, donations and sponsors.

 

Contact us
Subscribe to our monthly blog!