
Compiler Explorer - stmw
https://godbolt.org/
======
dang
If curious see also

2018
[https://news.ycombinator.com/item?id=18671993](https://news.ycombinator.com/item?id=18671993)

2016
[https://news.ycombinator.com/item?id=13182726](https://news.ycombinator.com/item?id=13182726)

2016 (a bit)
[https://news.ycombinator.com/item?id=12627295](https://news.ycombinator.com/item?id=12627295)

2016
[https://news.ycombinator.com/item?id=11671730](https://news.ycombinator.com/item?id=11671730)

2015
[https://news.ycombinator.com/item?id=9861294](https://news.ycombinator.com/item?id=9861294)

2015
[https://news.ycombinator.com/item?id=9085158](https://news.ycombinator.com/item?id=9085158)

2014
[https://news.ycombinator.com/item?id=7593109](https://news.ycombinator.com/item?id=7593109)

~~~
stmw
Oh, wow, I'm sorry! It was new to me, and it didn't flag it for me as a
duplicate.

It is so neat to have this smorgasbord without needing to install emulators or
containers with toolchains, or worse, acquire all of the relevant hardware as
in the old old days.

~~~
jcranmer
Obligatory xkcd: [https://xkcd.com/1053/](https://xkcd.com/1053/)

As a compiler writer, I find this useful when I have an "oh crap" moment and
need to find a simple C program to generate specific IR, or I need to find the
IR of a simple C program. Despite literally having a built tip-of-trunk
compiler that I'm working on. It's that convenient.

~~~
antoinealb
It is possible to hook custom compilers if you run compiler explorer locally,
at least I sort of remember using that in a previous role. Sounds like it
could be useful for your use case.

------
saagarjha
Godbolt links are always great for resolving "well actually" arguments about
how smart compilers are (which, as you can guess, are quite common on Hacker
News). No more "the compiler will do this"–if you're going to claim that, you
better have a Godbolt to back it up!

~~~
jchw
Something that surprised me: Compilers happily optimize pointer to member
function calls out if it knows definitely what the pointer points to, but not
if the pointer is to a virtual function. Recently figured that out thanks to a
slightly related debate on whether it would optimize the first case at all -
which I thought it would - but I’m surprised by the second case, since it’s
not like there’s a virtual dispatch to worry about once you’ve already taken a
reference - you’re already specifying exactly which one implementation you
want. Just one of many nuggets learned from Godbolt: apparently marking a
function virtual can kill optimizations even in cases where it seems like it
shouldn’t.

~~~
pjmlp
They can optimize pointer to a virtual function, but they need PGO data for
doing it.

The problem is that most devs don't bother with PGO.

~~~
saagarjha
In the absence of PGO data, some compilers nonetheless will speculate the
target of a virtual function call and optimize as such.

~~~
silon42
It could be a link time optimization to check if a single implementation is
used and then de-virtualize the calls.

------
strstr
Godbolt is great for comparing compiler versions (and compilers).

For example, you can see gcc's progression of efficiency for c atomics with
[https://godbolt.org/z/brsoEr](https://godbolt.org/z/brsoEr). If you increment
the gcc version number, you will see the (very slow) mfence disappear, and
xchg show up.

Then there is Clang at O3: If an int falls in the forest, and there is no one
around, was it ever incremented? No. The function turns into a bare ret.

~~~
kllrnohj
I'm actually quite surprised GCC's isn't also a bare ret. It of course is if
you replace the atomic_int with a regular int, I don't know why that wouldn't
be hitting the same optimizations. Yes it's atomic, but it's still an unused
local that doesn't escape the function.

~~~
hackingthenews
Clang/llvm also does heap ellision (removing unnecessary mallocs) while gcc
didn't last time I checked. I think the llvm devs allows themselves to have a
more practical understanding of the cpp standard.

~~~
BeeOnRope
Well we could check with godbolt:

[https://godbolt.org/z/1reM4s](https://godbolt.org/z/1reM4s)

GCC seems to eliminate them since at least 4.7.1.

~~~
hackingthenews
That's fair. Here is another example where it eliminates since 5.1.

[https://godbolt.org/z/3oGc3P](https://godbolt.org/z/3oGc3P)

------
necubi
Godbolt is such an amazing tool, and amazing that it's free.

For a random example from a few days ago, I wanted to understand how Rust
compiles various approaches to doing pairwise addition between a f64 vector
and a f32 vector:
[https://godbolt.org/z/9envsT](https://godbolt.org/z/9envsT). Profiling can
tell me which is fastest, but godbolt is really helpful for understanding why.

(Fun fact I learned recently, after years of using it: Godbolt is named after
its creator, Matt Godbolt [0]).

[0] [https://xania.org/MattGodbolt](https://xania.org/MattGodbolt)

~~~
stmw
Indeed, with such a splendid last name, it must take considerable humility to
avoid naming all of his projects and even variables with it. I doubt I would
be able to resist.

~~~
klipt
"As you can see, step 3 of the Godbolt algorithm increments the fourth Godbolt
variable by the gradient of the Godbolt network's Godbolt matrix..."

------
nemo1618
Throwing a bunch of code at godbolt and seeing it spit out a single 'mov' is
definitely on my list of Top 10 Most Satisfying Things:
[https://godbolt.org/z/bdn37q](https://godbolt.org/z/bdn37q)

------
drej
If you are wondering how it works, here is Matt Godbolt himself explaining it.
Surprisingly simple and cheap.
[https://www.youtube.com/watch?v=bSkpMdDe4g4&feature=youtu.be...](https://www.youtube.com/watch?v=bSkpMdDe4g4&feature=youtu.be&t=2852)

~~~
amelius
Will watch the video later, and I'm probably missing something but at this
moment I wonder what more it is than just running "gcc -S", which just spits
out assembly code.

By the way, what I think could make this tool more useful for the average user
is if the assembly were decompiled into another language (like C).

~~~
mattgodbolt
> By the way, what I think could make this tool more useful for the average
> user is if the assembly were decompiled into another language (like C).

It's somewhere in the ideas list... [https://github.com/compiler-
explorer/compiler-explorer/issue...](https://github.com/compiler-
explorer/compiler-explorer/issues/818)

Something similar is Andreas Fertig's awesome
[https://cppinsights.io/](https://cppinsights.io/)

------
xvilka
There is also a console client for it - cce[1]. It is written in Rust.

[1] [https://github.com/ethanhs/cce](https://github.com/ethanhs/cce)

------
sedatk
There is also one for .NET called SharpLab:
[https://sharplab.io/](https://sharplab.io/)

~~~
1f60c
How is this the first time I heard about this?

------
wtetzner
I recently discovered that it supports OCaml:
[https://ocaml.godbolt.org/](https://ocaml.godbolt.org/)

~~~
mattgodbolt
There's a language dropdown on any editor window at the top right. You'll see
we support quite a number of languages!

We also have a REST API:

$ curl -s
[https://godbolt.org/api/languages](https://godbolt.org/api/languages) Id |
Name go | Go c | C fortran | Fortran c++ | C++ cppx | Cppx assembly | Assembly
cuda | CUDA python | Python llvm | LLVM IR d | D ispc | ispc analysis |
Analysis nim | Nim rust | Rust clean | Clean pascal | Pascal haskell | Haskell
ada | Ada ocaml | OCaml swift | Swift zig | Zig⏎

~~~
wtetzner
Wow, that's awesome!

It's pretty cool being able to compare equivalent programs in different
languages.

------
maattdd
I did a submission 2 days ago about an impressively smart optimization in
Clang (GCC doesn't) from a badly linear isEven function :
[https://news.ycombinator.com/item?id=24049872](https://news.ycombinator.com/item?id=24049872)

------
imrehg
Enjoying the Python code deconstruction in GodBolt as well, feels like it
could help understand how things work in general (even though it's less of an
issue, than actual compiled languages).

------
fnord77
surprised at the big differences in various x86-64 compilers.

