CppCon 2017: Matt Godbolt “What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • CppCon.org
    -
    Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/Cpp...
    -
    In 2012, Matt and a colleague were arguing whether it was efficient to use the then-new-fangled range for. During the discussion a bash script was written to quickly compile C++ source and dump the assembly. Five years later and that script has grown into a website relied on by many to quickly see the code their compiler emits, to compare different compilers' code generation and behaviour, to quickly prototype and share code, and investigate the effect of optimization flags.
    In this talk Matt will not only show you how easy (and fun!) it is to understand the assembly code generated by your compiler, but also how important it can be. He'll explain how he uses Compiler Explorer in his day job programming low-latency trading systems, and show some real-world examples. He'll demystify assembly code and give you the tools to understand and appreciate how hard your compiler works for you.
    He'll also talk a little about how Compiler Explorer works behind the scenes, how it is maintained and deployed, and share some stories about how it has changed over the years. By the end of this session you'll be itching to take your favourite code snippets and start exploring what your compiler does with them.
    -
    Matt Godbolt: DRW, Senior Software Engineer
    Matt Godbolt is a software engineer with trading firm DRW, and the creator of the Compiler Explorer website. He is passionate about writing efficient code. He has previously worked at on mobile apps at Google, run his own C++ tools company and spent more than a decade making console games. When he's not hacking on Compiler Explorer, Matt enjoys writing emulators for old 8-bit computer hardware.
    -
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    *-----*
    *--*
    *-----*

ความคิดเห็น • 75

  • @ChrisCox-wv7oo
    @ChrisCox-wv7oo 2 ปีที่แล้ว +66

    Man, this presentation made me tear up. What a humble guy to have made and provide such a great service and to ask so little for it.
    Thanks Matt.

  • @MathNerdGamer
    @MathNerdGamer 6 ปีที่แล้ว +90

    32:28
    I'm sorry if this has been worked out by someone else in the comments before, but my old grad school instincts took "I haven't bothered to work it out" to mean "it's a good exercise to work out what this does," which is exactly what I did.
    The assembly in the output is just (a

  • @andreaslegomovies
    @andreaslegomovies 7 ปีที่แล้ว +144

    I love godbolting my code! I have learned so much from it!

  • @Mike-gs7eo
    @Mike-gs7eo 7 ปีที่แล้ว +47

    Really enjoyed this, had no idea quite how clever compilers were getting

  • @colin398
    @colin398 ปีที่แล้ว +2

    Its crazy how these talks are all free on youtube

  • @tthtlc
    @tthtlc 6 ปีที่แล้ว +6

    Yes, talking about execution of dynamically compiled codes. Few years back I wrote one directory traversal codes, online compiled it and managed to traversed the directory of the server. I highlighted this vulnerability to the site-owner.

  • @dementedchicken1
    @dementedchicken1 4 ปีที่แล้ว +8

    Excellent, excellent talk! Every single piece of information was valuable and entertainingly delivered.

  • @albertobastos
    @albertobastos 2 ปีที่แล้ว +7

    Fun & instructive, I'm glad I watched this. Terrific work, Matt.

  • @davidbrown552
    @davidbrown552 5 ปีที่แล้ว +1

    What a blast from the past. The good old days of programming a TRS-80 in assembly. Great lecture.

  • @JobvanderZwan
    @JobvanderZwan 6 ปีที่แล้ว +39

    The bit halfway through made me wonder if there are any benchmarks where people recompile old code for old hardware on new compilers (and new machines to save time), and see if the old stuff runs faster afterwards.

    • @AltamishM
      @AltamishM 6 ปีที่แล้ว +10

      Good point!
      I'm sure a fresh compile would benefit a decade old program, given how clever the compilers have become.

    • @AltamishM
      @AltamishM 6 ปีที่แล้ว +2

      narutofan9999 that's not very helpful haha XD

    • @commel
      @commel 2 ปีที่แล้ว +6

      That's actually a neat point of Java. Just run the same code on another CPU (with an jvm that understands it) and you are good to go to use new instructions the CPU offers.

    • @FatbocSlin
      @FatbocSlin 2 ปีที่แล้ว +3

      I saw that René compiled some basic program (gzip?) with the newest compiler on some ancient hardware th-cam.com/users/MoreReneRebe
      It was slower. I think it might have been after mitigations, but larger code might not benefit the smaller caches from back then.

    • @ewancrow7020
      @ewancrow7020 3 หลายเดือนก่อน

      I know it's been six years, but if you're still curious you should look into the efforts to improve the performance of mario 64

  • @emanuellandeholm5657
    @emanuellandeholm5657 ปีที่แล้ว +2

    @31:40 :D
    Also, I'm getting flashbacks from optimizing multiplication by 40 on the C64 in machine code. shift, shift, shift, store, shift, shift, add. The factor 40 comes up a lot because there are 40 bytes of characters per row.

  • @olegpatraschku3736
    @olegpatraschku3736 2 ปีที่แล้ว +3

    Thank you for the effort, it's an amazing tool and presentation was fun :)

    • @CppCon
      @CppCon  2 ปีที่แล้ว +2

      Glad it helped!

  • @angelcaru
    @angelcaru 5 หลายเดือนก่อน +1

    It has compiled your code!

  • @llothar68
    @llothar68 7 ปีที่แล้ว +12

    If you want to optimize virtual methods then use the "final" attribute on classes.

    • @MrSapps
      @MrSapps 5 ปีที่แล้ว

      with LTCG/LTO it still works as Matt said :)

    • @gim913
      @gim913 5 ปีที่แล้ว +1

      final does not guarantee in any way, that there won't be virtual calls...
      it's enough you have two classes both final, that implement a shared interface.

  • @xfreeman86
    @xfreeman86 7 หลายเดือนก่อน

    Small mistake @44:51. Matt says the problem is if you pass INT_MAX, but that will overflow in both cases. The actual edge case is close to sqrt(INT_MAX), which will overflow in the first case but not the second. I'm guessing that's the objection that was raised by the audience member.

  • @ameynarkhede8264
    @ameynarkhede8264 5 ปีที่แล้ว +10

    >Wrote a whole IRC client with UI and scripting support in assembly while in high school.

    • @pha1994
      @pha1994 5 ปีที่แล้ว +1

      Amey Narkhede That seems like a huge undertaking.

    • @GeorgeTsiros
      @GeorgeTsiros 2 ปีที่แล้ว +1

      @@pha1994 mostly drudgework
      writing assembly is nowhere near as incomprehensible as reading compiled code listings or, worse, disassembly

  • @pranjalagrawal969
    @pranjalagrawal969 6 ปีที่แล้ว +58

    Did he just call Google a 'small internet startup', there at 3:00?

    • @Nobody1707
      @Nobody1707 5 ปีที่แล้ว +29

      It was in 1998.

  • @wawangsf
    @wawangsf 4 ปีที่แล้ว

    Wish I had watched this a couple month ago!

  • @ZarviroffSerge
    @ZarviroffSerge 3 ปีที่แล้ว +1

    Brilliant!

  • @alexreinking
    @alexreinking 4 ปีที่แล้ว +6

    22:13 -- notice that GCC emits an extra instruction. Two rets one after the other. The one on line 12 could simply be deleted.

    • @jakobnissen5723
      @jakobnissen5723 4 ปีที่แล้ว +24

      Not neccesarily. The compiler may add redundant instructions in order to align the instructions in the instruction cache.

    • @alexreinking
      @alexreinking 3 ปีที่แล้ว +3

      @@jakobnissen5723 That is not the case here. The second-to-last `ret` on line 12 is already aligned; jumping to a redundant one after it is _completely_ pointless.

    • @VFPn96kQT
      @VFPn96kQT 2 ปีที่แล้ว +5

      @@alexreinking I think Matt explains it here: th-cam.com/video/w0sz5WbS5AM/w-d-xo.html

  • @EllAntares
    @EllAntares 3 หลายเดือนก่อน

    quite a few processors have popcount procedure

  • @SwordQuake2
    @SwordQuake2 4 ปีที่แล้ว +2

    Anyone have a link for the talk he mentions at 6:53?

  • @gim913
    @gim913 5 ปีที่แล้ว

    Once I heard you'll be using Intel syntax, I had to upvote.

  • @aliancemd
    @aliancemd 6 ปีที่แล้ว +5

    He mentioned all the languages except for Rust, which was supported for a pretty long time :(

  • @cavesalamander6308
    @cavesalamander6308 2 ปีที่แล้ว

    47:28 "... making the code efficient and *_most_* *_often_* *_correct_* too." ROFL!!!

  • @mbabuskov
    @mbabuskov 5 ปีที่แล้ว +19

    36:30 next time someone tells me Ubuntu is rock solid with no bugs

  • @AmeliusDex
    @AmeliusDex 7 ปีที่แล้ว +1

    Does anyone know if the other talk Matt kept talking about has (or will) be uploaded?

    • @MattGodbolt
      @MattGodbolt 7 ปีที่แล้ว +4

      The jsbeeb talk wasn't recorded, but a longer version is available here: th-cam.com/video/37jyHQT7fXQ/w-d-xo.html (if that's the one you meant)

  • @pavelponomarev1625
    @pavelponomarev1625 ปีที่แล้ว

    Is there a similar but for ARM 64bit architecture?

    • @canaDavid1
      @canaDavid1 หลายเดือนก่อน

      Most of the same optimizations are done for most architectures

  • @alchemira
    @alchemira 2 ปีที่แล้ว +2

    Wow, I could make a lot of videos on "what has my compiler done AGAINST me"

  • @henke37
    @henke37 7 ปีที่แล้ว +19

    LD_PRELOAD? Lame. Ptrace the thing instead and catch the syscalls no matter how they were reached.

    • @MattGodbolt
      @MattGodbolt 7 ปีที่แล้ว +27

      Thanks for the idea; I'll check it out

    • @ihatenumberinemail
      @ihatenumberinemail 6 ปีที่แล้ว +7

      No, use seccomp with a BPF filter.This is why seccomp exists.

    • @MattGodbolt
      @MattGodbolt 6 ปีที่แล้ว +19

      I'm currently considering both 'firejail' and 'isolate' which both use bpf filters and namespaces. Thanks all!

  • @p39483
    @p39483 2 ปีที่แล้ว +5

    This cleverness is starting to make template generics look like premature optimization. A C generic using (void*), (*)(), size, offset, stride will become a compile time generic due to cleverness. That is, if the parameters are constant, the compiler is just as happy to inline an indirect function call and unroll loops as if it were a with T::dostuff(). The C generic, however, has the advantage that it is simultaneously a runtime generic which can be called from Python or whatnot. So I'm amused by the irony in saying 1. compilers are clever while 2. advising the use of std:: template stuff. Said another way, templates tell the compiler to force compile time parameters when they are already clever enough to turn runtime parameters into compile time parameters when possible and defer to runtime what they must.

    • @SimonBuchanNz
      @SimonBuchanNz 2 ปีที่แล้ว +6

      If you're using templates for speed, your using them wrong. You use them because they will yell at you when you screw it up (eg you cast the state param wrong) or because you want to metaprogram on the types, for example using different parsing algorithms for different types automatically without the caller having to tell you which to use.
      Also keep in mind that classically you lose all the interprocedual optimization when you call across compilation units, so remember to use global optimization.
      Also it's generally also trivial to get a C ABI version of a template by simply passing the instantiation parameters to it!

  • @peterSobieraj
    @peterSobieraj ปีที่แล้ว

    OMG. I though I'm so clever because I have made script that delete unused functions and variables.
    Dunning Kruger effect.

  • @xrafter
    @xrafter 4 ปีที่แล้ว

    31:05
    Lol

  • @LaurentLaborde
    @LaurentLaborde 7 ปีที่แล้ว

    javascript x86 emulator exists (eg : v86) but they are not as complete as you'd like to :D (ho nvm, it was pointed out in Q&A)

  • @LemonChieff
    @LemonChieff 6 ปีที่แล้ว +1

    :D

  • @bartekkko
    @bartekkko 7 ปีที่แล้ว +1

    so the main takeaway is to leave gcc and use clang instead? bit of a shame because compiling in gcc takes significantly less typing (g++ file.cpp) than compiling in clang (clang++ file.cpp) and that only gets you c++98 in clang

    • @cH3rtzb3rg
      @cH3rtzb3rg 7 ปีที่แล้ว +20

      If the time you need to type to call the compiler is your bottleneck, just stay with g++ (or make an 1-letter alias for your favorite compiler + arguments).
      If you actually care about runtime performance, set up your build-system to compile with different compilers and do regular benchmarks.

    • @bartekkko
      @bartekkko 7 ปีที่แล้ว +7

      i was making a joke in that second sentence, really. I'm a physics student so I mostly write small single-file prototype things and it's not worth bringing out the makefile for that. still, I'll probably do that aliasing thing, later, when i can be arsed

    • @nameguy101
      @nameguy101 7 ปีที่แล้ว +6

      Fun fact: It is literally never worth bringing out the makefile. Makefiles and build systems of all kinds are shit.

    • @meekrab9027
      @meekrab9027 7 ปีที่แล้ว +9

      Just alias g++ to clang++ like Apple does by default. :^)

    • @llothar68
      @llothar68 7 ปีที่แล้ว +2

      I aliased my build system to "r" for run, beat me

  • @kaboissonneault
    @kaboissonneault 7 ปีที่แล้ว

    First

    • @bietola75
      @bietola75 7 ปีที่แล้ว +1

      Doomsayer_Hazel first to reply to first

  • @ongamex
    @ongamex 7 ปีที่แล้ว +2

    I see no point of this lecture. Every year we have that talk multiple times.

    • @dipi71
      @dipi71 7 ปีที่แล้ว +15

      That exact talk? Including the new stuff and Q&A? For example, I didn’t know about the JS-based x86 emulator.

    • @gabrielb.2886
      @gabrielb.2886 6 ปีที่แล้ว +32

      He's not talking only for you. I hope you don't complain every time somebody says something you already know.
      I've never listened to such a talk before and I'm glad that it is not 5 year's old.
      BTW, many people still think that C++ abstractions have to be slower than C code and assembly is the fastest you can get while it is obvious that on any reasonably complex program, the compiler outperforms the best assembly writers. Still a lot of new tricks to learn to old dogs, which you apparently aren't ;)

    • @Runoratsu
      @Runoratsu 6 ปีที่แล้ว +9

      And as long as people don't learn, there shall be more!

  • @makerofstartup7902
    @makerofstartup7902 5 ปีที่แล้ว

    waste of time