Understanding Compiler Optimization - Chandler Carruth - Opening Keynote Meeting C++ 2015

Meeting Cpp · 146K views · 2015 · 1:50:15

Open on YouTube

Compilers, interpreters & program analysisC, C++, Rust, Go & systems languagesSoftware performance & optimizationtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about what compilers can and cannot optimize, such as const, aliasing, goto, function extraction and parameter passing.

Recommended by 5 people across 6 different months, 2014 to 2019.

What it is

Chandler Carruth, an LLVM/Clang developer, gives the opening keynote at Meeting C++ 2015 on how optimizing compilers actually see and transform C++ code. It runs just under two hours and covers topics such as aliasing, const, inlining, vectorization and how code structure affects optimization.

What people said

Recommends atlaste · 2016-02 · Stack Overflow · on “Compilers evaluating constant expressions”

I suggest you watch this video: [this video] . Chandler explicitly notes 'const_cast' there and the implication on how const is handled -- that said, things like data flow are quite complex to predict, so best to simply test it.

Recommends nwp · 2016-05 · Stack Overflow · on “Does extracting functions affect performance?”

Good software design makes the software easier to optimize, making it perform better. I see no balance here, performance and good design go hand in hand. I recommend you invest 2 hours to learn about compiler optimizations [this video] .

Recommends Jesper Juhl · 2017-07 · Stack Overflow · on “C++ Can constant class data be optimized out of class by compiler?”

@user2079303 I think you'll find this interesting: Understanding Compiler Optimization - Chandler Carruth - Opening Keynote Meeting C++ 2015 [this video] - and I think you'll find that he disagrees with you; `const` is basically *useless* to the optimizer.

Recommends Stephen Newell · 2018-02 · Stack Overflow · on “Encapsulating a large number of parameters in C++14”

A lot of this is opinion-based, but I'll throw my hat in the ring. Object oriented style Not a fan for your use. Since the only operation you support is compute , it's effectively operator () by a different name. operator () means you play nicely with the algorithm header, so this is an inferior solution to the Functor and Functional styles. In addition, you may have code that performs more poorly with this solution. The entire talk is worth watching if you're curious, but Chandler Carruth (an LLVM/Clang developer) explains how the compiler sees your code [this video] (skip to about 1:32:37, but the whole talk is great). The gist of it is that you have an implicit pointer in this implementation,

Recommends Stephen Newell · 2018-03 · Stack Overflow · on “Are compilers not allowed to assume const-ref parameters will stay const?”

A const ref may alias the same memory as a non- const variable that's modified in another thread. An interesting talk where this comes up (by an LLVM/Clang developer) is available here: [this video]

Recommends JVApen · 2019-06 · Stack Overflow · on “why in the source code __find_if's __trip_cout calculate as (__last-__first) >> 2”

I can't comment on the exact reason to introduce this, however, it does look like a classical vectorizing technique. To code seems to been written in a way that doing the predicate on the 4 consecutive elements would be more efficient. I can recommend Understanding Compiler Optimization - Chandler Carruth - Opening Keynote Meeting C++ 2015 [this video] as background information on compiler optimizations as it explains in more detail why it ain't the best idea to do so. Some pointers to take away for it (spoilers) are: Your predicate could be inlined, if it would be a complex lambda, the vectorization might not be useful and the loop body might not fit in the execution cache, which could slow down

Cites atlaste · 2014-06 · Stack Overflow · on “What is the use of labels in C#?”

Why is it there? The reason is actually pretty simple. Assembler and IL don't support advanced instructions like for , do , break , continue and while . Implicitly, code that loops (or more generally: branches) is compiled to branches. goto and label is a very literal branch. Note that you simply cannot implement all the contraptions that I can think of easily without a 'goto'- even though there are usually better alternatives (at this point I'd like to point out that there are Design Patterns like the State pattern ). If you design a language like C#, which is kind-of the "high level assembler for IL", it only makes sense to support at least everything there is in IL. Here, branch / label i

Cites atlaste · 2016-02 · Stack Overflow · on “What is the use of labels in C#?”

"There is nothing wrong with labels and goto's in themselves. " -- not true. Optimizers in compilers (or in your .NET runtime) act on the fact that code is _predictable_. If you have unpredictable code, your optimizer won't be able to do anything useful with it. While your example will simply be evaluated as a loop, there are a ton of cases where labels and goto's will lead to unpredictable (and hence un-optimizable) results. This video explains a thing or two of how this actually works: [this video]

Cites atlaste · 2016-03 · Stack Overflow · on “how to break first foreach loop from second nested foreach loop in c#”

Quick answer: foreach(//do some stuff) { foreach(//do some stuff) { if(//check some condition) { goto end; // I'd probably add a comment here } } // *1 } end: {} // the rest of your code. But, but SESE... SESE violations are violations of the Single Entry Single Exit principle. It's quite easy to fix, by using an extra condition: bool found = false; for (int i=0; i<foo.Count && !found; ++i) { for (int j=0; j<bar.Count; ++j) { if (...) { found = true; } } // *1 if (!found) { ... } } So why use a GOTO here? I believe that creating proper, maintainable code means that you use the language constructs that most closely describes your intent. "Code" here is always composed of two things: Control f

Cites JVApen · 2018-07 · Stack Overflow · on “change function to a class with method for setting input variables of function”

Chandler Carruth gave a presentation which explicitly mentions the new piece of code as ruining the compilers ability to optimize. ( [this video] at 1h30)