Compiler Explorer

116 points by rbanffy on 2024-05-14 | 17 comments

Automated Summary

Compiler Explorer (CE) is a useful tool for anyone interested in compilers or computer architecture. It allows users to enter source code, and the site immediately shows the compiled output in a panel on the right. CE supports 69 source languages, over 2000 compilers, and a wide range of target architectures. CE is open-source, and most people will use the online version, which is free and supported by donations and sponsorships. CE is a great tool for learning assembly language, comparing the output from different compilers, and understanding code and how it runs. It can also show the LLVM Intermediate Representation and provides access to tools like the LLVM Machine Code Analyzer. CE can show the bytecode generated by interpreted languages and even provides a simple complete IDE.


klelatti on 2024-05-14

Author here. Thanks for all the upvotes - and Compiler Explorer is awesome - but I’m not sure this should be on the front page as it’s mostly a collection of links. @dang

sfink on 2024-05-14

I disagree. I found it to be a very nice approachable high-level walkthrough of what CE can do. I've used CE for various things, but still learned about several capabilities that I didn't know about and can make use of.

And calling something like this just "a collection of links"... have you seen the crap that passes for a web page on today's web? I'll take a manually curated collection of links with some useful commentary any day over the AI-generated "here are 30 answers to your question and several similar questions, with repeats and contradictory answers thrown in for free" things I find all over the place.

gwbas1c on 2024-05-14

> but I’m not sure this should be on the front page as it’s mostly a collection of links

I disagree. A lot of time there's a link to a project on Hacker News without any kind of about page. I significantly prefer to go to an about / demo page that tells me about the tool, with links to the tool itself.

> Disclaimer: We are now entering a ‘rabbit hole’ that may consume several hours of your time.

I really enjoyed skimming the article; seeing your results instead of taking the time to learn how to use the tool and generate similar results.

almostgotcaught on 2024-05-14

I have never seen it called anything other than godbolt and rightfully so because it's a fantastic, free, open source tool (so that guy deserves all the notoriety furnished by everyone knowing the tool by only his name).

p0w3n3d on 2024-05-14

For me (on every domain highlighted in github) it's

which renders the code unreadable

p0w3n3d on 2024-05-14

apparently those are fonts 'Liberation mono' and 'Consolas' on macos

Lockal on 2024-05-15

> may consume several hours of your time

Several hours? More like few lives, the stack is just too big to deal within a single life :)

And the rabbit hole goes even deeper with external tools like and mirrored versions for obscure architectures

orlp on 2024-05-14

As a low-level performance-oriented engineer I use the Compiler Explorer almost every day. It is a great tool, give it a try.

jlarocco on 2024-05-14

Is Common Lisp the only language with this built-in?

    CL-USER> (defun foo (a b)
               (declare (optimize (speed 3))
                        (type fixnum a b))
               (+ a b))
    CL-USER> (disassemble #'foo)
    ; disassembly for FOO
    ; Size: 23 bytes. Origin: #x1002230744                        ; FOO
    ; 44:       4801FA           ADD RDX, RDI
    ; 47:       48D1E2           SHL RDX, 1
    ; 4A:       710A             JNO L0
    ; 4C:       48D1DA           RCR RDX, 1
    ; 4F:       FF1425F00C1050   CALL [#x50100CF0]                ; SB-VM::ALLOC-SIGNED-BIGNUM-IN-RDX
    ; 56: L0:   C9               LEAVE
    ; 57:       F8               CLC
    ; 58:       C3               RET
    ; 59:       CC10             INT3 16                          ; Invalid argument count trap
The Emacs Lisp mode has support for it, too - I have Alt-F8 open the disassembly of the current function in a new window next to the current window.

nick__m on 2024-05-14

Julia has it it's called code_native

guenthert on 2024-05-15

you were probably looking for

    (defun foo (a b)
      (declare (optimize (speed 3))
               (type fixnum a b))
      (the fixnum (+ a b)))

jlarocco on 2024-05-16

Heh, I didn't really think about it, but to finish out the example:

    CL-USER> (defun foo (a b)
      (declare (optimize (speed 3))
               (type fixnum a b))
      (the fixnum (+ a b)))
    CL-USER> (disassemble #'foo)
    ; disassembly for FOO
    ; Size: 22 bytes. Origin: #x101127E9B4                        ; FOO
    ; B4:       4801FA           ADD RDX, RDI
    ; B7:       488BC2           MOV RAX, RDX
    ; BA:       48D1E0           SHL RAX, 1
    ; BD:       7006             JO L0
    ; BF:       48D1E2           SHL RDX, 1
    ; C2:       C9               LEAVE
    ; C3:       F8               CLC
    ; C4:       C3               RET
    ; C5: L0:   CC59             INT3 89                          ; OBJECT-NOT-FIXNUM-ERROR
    ; C7:       0A               BYTE #X0A                        ; RDX(s)
    ; C8:       CC10             INT3 16                          ; Invalid argument count trap
And to optimize away everything:

    CL-USER> (defun foo (a b)
      (declare (optimize (speed 3) (safety 0) (debug 0) (space 3))
               (type fixnum a b))
      (the fixnum (+ a b)))
    CL-USER> (disassemble #'foo)
    ; disassembly for FOO
    ; Size: 6 bytes. Origin: #x101127F026                         ; FOO
    ; 6:       4801FA           ADD RDX, RDI
    ; 9:       C9               LEAVE
    ; A:       F8               CLC
    ; B:       C3               RET

guenthert on 2024-05-16

Indeed. Keep in mind though that the disassembly begins at an offset and doesn't contain the function header, i.e. FOO is quite a bit larger than 6 bytes.

dang on 2024-05-14

Related. Others?

The Compiler Explorer Story – Matt Godbolt - - April 2024 (5 comments)

Matt Godbolt: Five things you didn't realise your CPU did for you - - Oct 2022 (16 comments)

Compiler Explorer - - Oct 2022 (107 comments)

Happy 10th Birthday Compiler Explorer - - May 2022 (20 comments)

Compiler Explorer - - Aug 2020 (48 comments)

Compiler Explorer - - Dec 2018 (44 comments)

How it works: Compiler Explorer - - Feb 2018 (25 comments)

Godbolt: Enter C, get Assembly - - Dec 2016 (151 comments)

Compiler Explorer – now with side-by-side compiler comparison - - Oct 2016 (6 comments)

Compiler Explorer - - May 2016 (38 comments)

Interactive C++ Assembly Explorer - - July 2015 (8 comments)

See What Your C Function Compiles To - - June 2015 (2 comments)

Interactive Go source to assembly - - Feb 2015 (12 comments)

Nibble sort competition winner on GCC Explorer - - Feb 2015 (1 comment)

C/C++ Fiddle - - April 2014 (40 comments)

Interactively watch how your C++ gets compiled. - - May 2012 (1 comment)

guenthert on 2024-05-15

Well, as awesome as the godbolt explorer and similar tools are, I see the danger of them aiding in premature optimization.

gradientsrneat on 2024-05-15

Wait, people use them for benchmarks? I just use them to find quick workarounds to incrutable compiler errors and language idiosyncracies. I wouldn't trust a web app for any serious optimization testing (unless the web was my target platform).

menaerus on 2024-05-15

I think that Amdahl's law explains it much more succinctly.