SlideShare a Scribd company logo
Welcome to my short presentation with a long title :)
I'm currently working for Nokia where we're moving part of software from base station into cloud –
hence the name of our team – Miotacze Piorunów.
I think that direct translation would be “lighting throwers”, but I'm not sure.
We even have a flag! :)
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
How do we perceive compilers?
Most of the time when we think about them we have similar picture in our minds.
There's a back box, which is being feed with C++ in order to produce some binary - presumably for
x86 machines.
Besides the fact the picture is oversimplified, there's nothing wrong with it. However, if we make a
little step towards reality...
… we get something like this.
Not only C and C++ are supported by C++ compilers.
Other languages like Google Go or Objective-C are also supported.
Afaik GCC support 9 languages out of the box.
And of course you can provide an implementation for your own (custom) language.
Same thing can be said about possible targets..
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
It's kind of insane, but GCC supports more than 60 targets! That's just amazing.
Cheerp is an interesting example of how flexible compilers can be.
It's modified clang and it allows you to write isomorphic web apps in C++!
The funny thing is that instead of producing a binary it produces… JavaScript.
How it's even possible? Mainly because modular architecture.
The picture is of course oversimplified, as almost everything in the presentation.
There's clear split between the front-end and the back-end.
Front-end responsibility is to convert input language into “an internal representation”.
The IR is an interface between the two guys.
Most of the things happen in the backend – optimizations, code generation, more optimizations etc.
Frontent is relatively simple, when compared to the backend.
Lots of problems (NP-hard, NP-complete) are solved by compilers. They're huge and complex.
How do we control their behavior, then?
With command line switches!
When we're learning C++ we are simultaneously learning how to use our favorite compiler. We start
with knowing little things like -o.
Then we discover how to do much more: static libs, shared libs.
Then how to control verbosity, optimizations and so on.
Eventually we become ninjas. Detaching STD lib or preprocessor? Why not :)
I have made small research to see how customizable GCC is compared to other common tools.
There's huge gap between GCC and other Linux tools.
The only two players that are close are Google Chrome (which is like a virtual machine btw) and git. I
wasn't surprised about git :)
I know it's not a perfect measure, but it definitely tells us something – compilers are highly
customizable.
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
Similarly to previous image, the relation between C++ standard and compilers seems to be simple.
There's one international specification which is being implemented by multiple vendors.
Hence we have different compilers.
Easy, isn't it?
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
If you look for implementation-defined stuff in the standard, you're going to find more than 100 items.
This leaves room for compilers to differ.
However, there's one more evil thing out there.
Yes, you're right.
C++ standard (C++14 working draft to be precise) contains over 100 occurrences of “is undefined”
phrase. Crazy.
Even if you're seasoned programmer you are likely to introduce bugs to applications. The worst thing is
that you're not gonna notice that until something breaks in production.
Programming in C++ used to be like going into dark cave without a flashlight, knowing that you may
be bitten by a snake.
Fortunately, the situation is getting better.
Modern compilers are shipped with tools that help to find different kind of problems.
One of these tools is undefined behavior sanitizer.
The idea behind UBSan is that the compiler instruments the code so that in runtime it can detect Ubs.
This isn't without cost. It slows down execution ~ 2-3x. It is definitely good to give it a try in tests,
though.
Programmer, for different kinds of UB can choose the behavior – trap instruction, print & continue, or
print & exit.
With UBSan there's simply more control.
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
Optimizations are everywhere.
They do start in the fronend (AST), then they are apparent at IR phase, GEN phase. Even after creating
object files we have optimizations.
Special information is encoded within object files so the link time optimizer can kick in and optimize at
the top level.
There are also run-time optimizations, like profile guided optimization or recently announced
efficiency sanitizer which hopefully will join C++ world.
Other important thing about optimizing is that we tend to underestimate the compiler. We also try to
outsmart compiler sometimes.
E.g. instead of traditional approach to swapping one can use three XOR assignments.
Looks hacky, but is it faster indeed?
Apparently not.
A lot of knowledge has been put into compilers by smart people.
They know about CPU caches and extensions (like e.g. SIMD).
Also, not all CPUs are perfect. There may be a performance in some revision of some CPU. It's likely
that the compiler will already know about it and will avoid this instruction whilst generating code.
What about us? Do we know? Not necessarily.
Compilers look at the code generations from multiple perspectives. It's simply hard to outsmart them.
And even if you can, then why wouldn't you improve them in a first place anyway?
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)
I've already mentioned UBSan.
We have more than that.
AddressSanitizer and MemorySanitizer allows us to spot errors related to memory (e.g. reading
uninitialized memory or ordinary memory leak).
Our apps can be subject of cracking. Some bad people can try to hijack the flow so the program does
unintended things. For protection we can use ControlFlowSanitizer. It will look for situations that could
possibly be used by hacker to take control over execution (e.g. return-oriented programming).
If our application is multi-threaded we can use ThreadSanitizer to find deadlocks, data races, etc.
And finally, recently annoucned EfficiencySanitizer will look how the performance can be improved.
E.g. it will look for write-after-write, possible reordering of fields in struct to improve cache
friendliness etc...
All of the sanitizers are likely to slow down the program.
Modern compilers are also shipped with auxiliary tools, like presented on the slide.
• Reformatting the code → clang-format
• C++ linter → clang-tidy
• Suggestions in IDE → clang-complete
• Static analysis tool → clang-analyzer
All of these tools have common part – they're using compiler as a library.
No C++ parsing is done by these tools (that was always tricky part). All of the information is obtained
from the compiler so 100% accuracy is achieved.
The list is not complete! Go read your compiler's documentation :)
It's both funny and sad that we've been having runtime debuggers for years without single compile-time
debugger.
Now we have two.
They can be used to see how templates are instantiated and memoized.
Can be quite useful in case we want to e.g. decrease compilation time or see with 100% accuracy how
templates are instantiated in our project.
Other example of how compiler can be used as libraries is Synth tool which was announced few weeks
ago.
Purpose of the tool is to convert C++ code into hyper-linked colorful HTML.
Hyperlinks of standard containers, classes, etc. redirect to C++ reference page.
And finally a tool that isn't actually strictly related to compilers, but it lives near by.
STOKE is a tool that can possibly improve performance of single functions (10, 11 instructions).
Functions' codes (little programs in fact) are treated as if they were single points in multi-dimensional
space.
Changing instruction order, adding instructions, removing them, or changing order of arguments – it's
all a move in such a space.
Compilers start in some area where instruction set composes a valid program. Then they optimize the
code within the area.
It's likely than they won't be able to jump to other (distant) areas where the code is still valid and has
better performance.
And that's the niche STOKE is focused on… maybe it's not mature now but who knows – maybe it's a
future of code optimization?
What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)

More Related Content

What's hot (19)

PDF
Go language presentation
paramisoft
 
PDF
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov
 
PPT
Python and data analytics
Shree M.L.Kakadiya MCA mahila college, Amreli
 
ODP
Bridging Ousterhout's Dichotomy
guest2838a0
 
PDF
Are 64-bit errors real?
PVS-Studio
 
PDF
Research paper on python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
PDF
C plus plus for hackers it security
CESAR A. RUIZ C
 
PDF
Why you should care about Go (Golang)
Aaron Schlesinger
 
PPTX
difference between c c++ c#
Sireesh K
 
PDF
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Codemotion
 
PDF
Beginning development in go
Equaleyes Solutions Ltd.
 
PPT
G W T(2)
tomcoh
 
PDF
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
PDF
Algorithm pseudocode flowchart program notes
ArghodeepPaul
 
PDF
Ruby
tutorialsruby
 
PPTX
Python vs MATLAB: Which one is the best language
Stat Analytica
 
PDF
F# for Scala developers
Alfonso Garcia-Caro
 
PDF
Harmonic Stack for Speed
Yung-Yu Chen
 
PPTX
Simplifying training deep and serving learning models with big data in python...
Holden Karau
 
Go language presentation
paramisoft
 
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov
 
Bridging Ousterhout's Dichotomy
guest2838a0
 
Are 64-bit errors real?
PVS-Studio
 
Research paper on python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
C plus plus for hackers it security
CESAR A. RUIZ C
 
Why you should care about Go (Golang)
Aaron Schlesinger
 
difference between c c++ c#
Sireesh K
 
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Codemotion
 
Beginning development in go
Equaleyes Solutions Ltd.
 
G W T(2)
tomcoh
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
Algorithm pseudocode flowchart program notes
ArghodeepPaul
 
Python vs MATLAB: Which one is the best language
Stat Analytica
 
F# for Scala developers
Alfonso Garcia-Caro
 
Harmonic Stack for Speed
Yung-Yu Chen
 
Simplifying training deep and serving learning models with big data in python...
Holden Karau
 

Similar to What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016) (20)

PDF
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
PPTX
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
PPTX
Computer and multimedia Week 1 Windows Architecture.pptx
fatahozil
 
ODP
The D Programming Language - Why I love it!
ryutenchi
 
PDF
Programming Languages #devcon2013
Iván Montes
 
PDF
Building reusable libraries
Felix Morgner
 
PDF
Introduction to Compiler Development
Logan Chien
 
PPT
Fp201 unit1 1
rohassanie
 
PPTX
Compiler optimizations based on call-graph flattening
CAFxX
 
PDF
Modern C++
Michael Clark
 
PDF
01 overview
Suresh Kumar
 
PDF
01 overview
Suresh Kumar
 
PDF
C++ Restrictions for Game Programming.
Richard Taylor
 
PDF
Programming in C++
Lindsey Anderson
 
PDF
What is code - Part 1
Nandeep Mali
 
PPTX
Intel JIT Talk
iamdvander
 
PPTX
Modern C++
Richard Thomson
 
PPTX
Compiler presentaion
Shady A. Alefrangy
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
Computer and multimedia Week 1 Windows Architecture.pptx
fatahozil
 
The D Programming Language - Why I love it!
ryutenchi
 
Programming Languages #devcon2013
Iván Montes
 
Building reusable libraries
Felix Morgner
 
Introduction to Compiler Development
Logan Chien
 
Fp201 unit1 1
rohassanie
 
Compiler optimizations based on call-graph flattening
CAFxX
 
Modern C++
Michael Clark
 
01 overview
Suresh Kumar
 
01 overview
Suresh Kumar
 
C++ Restrictions for Game Programming.
Richard Taylor
 
Programming in C++
Lindsey Anderson
 
What is code - Part 1
Nandeep Mali
 
Intel JIT Talk
iamdvander
 
Modern C++
Richard Thomson
 
Compiler presentaion
Shady A. Alefrangy
 
Ad

More from Sławomir Zborowski (9)

PDF
C++ Undefined Behavior (Code::Dive 2016)
Sławomir Zborowski
 
PDF
What every C++ programmer should know about modern compilers (w/o comments, A...
Sławomir Zborowski
 
PDF
Algorithms for Cloud Computing
Sławomir Zborowski
 
PDF
C++17 - the upcoming revolution (Code::Dive 2015)/
Sławomir Zborowski
 
PDF
More functional C++14
Sławomir Zborowski
 
PDF
Boost.Python - domesticating the snake
Sławomir Zborowski
 
PDF
Metaprogramming in C++ - from 70's to C++17
Sławomir Zborowski
 
PDF
Boost Multi Index
Sławomir Zborowski
 
PDF
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
C++ Undefined Behavior (Code::Dive 2016)
Sławomir Zborowski
 
What every C++ programmer should know about modern compilers (w/o comments, A...
Sławomir Zborowski
 
Algorithms for Cloud Computing
Sławomir Zborowski
 
C++17 - the upcoming revolution (Code::Dive 2015)/
Sławomir Zborowski
 
More functional C++14
Sławomir Zborowski
 
Boost.Python - domesticating the snake
Sławomir Zborowski
 
Metaprogramming in C++ - from 70's to C++17
Sławomir Zborowski
 
Boost Multi Index
Sławomir Zborowski
 
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
Ad

Recently uploaded (20)

PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PDF
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PPTX
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
PPTX
site survey architecture student B.arch.
sri02032006
 
PPTX
drones for disaster prevention response.pptx
NawrasShatnawi1
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
PDF
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
site survey architecture student B.arch.
sri02032006
 
drones for disaster prevention response.pptx
NawrasShatnawi1
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 

What every C++ programmer should know about modern compilers (w/ comments, ACCU 2016)

  • 1. Welcome to my short presentation with a long title :)
  • 2. I'm currently working for Nokia where we're moving part of software from base station into cloud – hence the name of our team – Miotacze Piorunów. I think that direct translation would be “lighting throwers”, but I'm not sure. We even have a flag! :)
  • 5. How do we perceive compilers? Most of the time when we think about them we have similar picture in our minds. There's a back box, which is being feed with C++ in order to produce some binary - presumably for x86 machines. Besides the fact the picture is oversimplified, there's nothing wrong with it. However, if we make a little step towards reality...
  • 6. … we get something like this.
  • 7. Not only C and C++ are supported by C++ compilers.
  • 8. Other languages like Google Go or Objective-C are also supported.
  • 9. Afaik GCC support 9 languages out of the box. And of course you can provide an implementation for your own (custom) language.
  • 10. Same thing can be said about possible targets..
  • 12. It's kind of insane, but GCC supports more than 60 targets! That's just amazing.
  • 13. Cheerp is an interesting example of how flexible compilers can be. It's modified clang and it allows you to write isomorphic web apps in C++! The funny thing is that instead of producing a binary it produces… JavaScript.
  • 14. How it's even possible? Mainly because modular architecture. The picture is of course oversimplified, as almost everything in the presentation. There's clear split between the front-end and the back-end. Front-end responsibility is to convert input language into “an internal representation”. The IR is an interface between the two guys.
  • 15. Most of the things happen in the backend – optimizations, code generation, more optimizations etc. Frontent is relatively simple, when compared to the backend. Lots of problems (NP-hard, NP-complete) are solved by compilers. They're huge and complex. How do we control their behavior, then?
  • 16. With command line switches! When we're learning C++ we are simultaneously learning how to use our favorite compiler. We start with knowing little things like -o. Then we discover how to do much more: static libs, shared libs. Then how to control verbosity, optimizations and so on. Eventually we become ninjas. Detaching STD lib or preprocessor? Why not :)
  • 17. I have made small research to see how customizable GCC is compared to other common tools. There's huge gap between GCC and other Linux tools. The only two players that are close are Google Chrome (which is like a virtual machine btw) and git. I wasn't surprised about git :) I know it's not a perfect measure, but it definitely tells us something – compilers are highly customizable.
  • 19. Similarly to previous image, the relation between C++ standard and compilers seems to be simple. There's one international specification which is being implemented by multiple vendors. Hence we have different compilers. Easy, isn't it?
  • 21. If you look for implementation-defined stuff in the standard, you're going to find more than 100 items. This leaves room for compilers to differ. However, there's one more evil thing out there. Yes, you're right.
  • 22. C++ standard (C++14 working draft to be precise) contains over 100 occurrences of “is undefined” phrase. Crazy.
  • 23. Even if you're seasoned programmer you are likely to introduce bugs to applications. The worst thing is that you're not gonna notice that until something breaks in production. Programming in C++ used to be like going into dark cave without a flashlight, knowing that you may be bitten by a snake. Fortunately, the situation is getting better.
  • 24. Modern compilers are shipped with tools that help to find different kind of problems. One of these tools is undefined behavior sanitizer. The idea behind UBSan is that the compiler instruments the code so that in runtime it can detect Ubs. This isn't without cost. It slows down execution ~ 2-3x. It is definitely good to give it a try in tests, though. Programmer, for different kinds of UB can choose the behavior – trap instruction, print & continue, or print & exit.
  • 25. With UBSan there's simply more control.
  • 27. Optimizations are everywhere. They do start in the fronend (AST), then they are apparent at IR phase, GEN phase. Even after creating object files we have optimizations. Special information is encoded within object files so the link time optimizer can kick in and optimize at the top level. There are also run-time optimizations, like profile guided optimization or recently announced efficiency sanitizer which hopefully will join C++ world.
  • 28. Other important thing about optimizing is that we tend to underestimate the compiler. We also try to outsmart compiler sometimes. E.g. instead of traditional approach to swapping one can use three XOR assignments. Looks hacky, but is it faster indeed?
  • 30. A lot of knowledge has been put into compilers by smart people. They know about CPU caches and extensions (like e.g. SIMD). Also, not all CPUs are perfect. There may be a performance in some revision of some CPU. It's likely that the compiler will already know about it and will avoid this instruction whilst generating code. What about us? Do we know? Not necessarily. Compilers look at the code generations from multiple perspectives. It's simply hard to outsmart them. And even if you can, then why wouldn't you improve them in a first place anyway?
  • 32. I've already mentioned UBSan. We have more than that. AddressSanitizer and MemorySanitizer allows us to spot errors related to memory (e.g. reading uninitialized memory or ordinary memory leak). Our apps can be subject of cracking. Some bad people can try to hijack the flow so the program does unintended things. For protection we can use ControlFlowSanitizer. It will look for situations that could possibly be used by hacker to take control over execution (e.g. return-oriented programming). If our application is multi-threaded we can use ThreadSanitizer to find deadlocks, data races, etc. And finally, recently annoucned EfficiencySanitizer will look how the performance can be improved. E.g. it will look for write-after-write, possible reordering of fields in struct to improve cache friendliness etc... All of the sanitizers are likely to slow down the program.
  • 33. Modern compilers are also shipped with auxiliary tools, like presented on the slide. • Reformatting the code → clang-format • C++ linter → clang-tidy • Suggestions in IDE → clang-complete • Static analysis tool → clang-analyzer All of these tools have common part – they're using compiler as a library. No C++ parsing is done by these tools (that was always tricky part). All of the information is obtained from the compiler so 100% accuracy is achieved. The list is not complete! Go read your compiler's documentation :)
  • 34. It's both funny and sad that we've been having runtime debuggers for years without single compile-time debugger. Now we have two. They can be used to see how templates are instantiated and memoized. Can be quite useful in case we want to e.g. decrease compilation time or see with 100% accuracy how templates are instantiated in our project.
  • 35. Other example of how compiler can be used as libraries is Synth tool which was announced few weeks ago. Purpose of the tool is to convert C++ code into hyper-linked colorful HTML. Hyperlinks of standard containers, classes, etc. redirect to C++ reference page.
  • 36. And finally a tool that isn't actually strictly related to compilers, but it lives near by. STOKE is a tool that can possibly improve performance of single functions (10, 11 instructions). Functions' codes (little programs in fact) are treated as if they were single points in multi-dimensional space. Changing instruction order, adding instructions, removing them, or changing order of arguments – it's all a move in such a space. Compilers start in some area where instruction set composes a valid program. Then they optimize the code within the area. It's likely than they won't be able to jump to other (distant) areas where the code is still valid and has better performance. And that's the niche STOKE is focused on… maybe it's not mature now but who knows – maybe it's a future of code optimization?