Skip to content
0:00 / 0:00

#llvm#inlining#valuesemantics

2013 Keynote: Chandler Carruth: Optimizing the Emergent Structures of C++

CppNow

39K subscribers • 42 videos on Hallway • Conference channel

YouTube
  • Recommended by 2 people: 1 on Hacker News and 1 curated list.
  • Presented by Chandler Carruth, Google compiler team lead

Advanced. Deep dive for people who know the field.

34K views13 years ago2 recommendations

Programming languagesSystems programmingC++Compilers and interpretersPerformance and optimization

Chandler Carruth explains how modern compilers optimize complex C++ software systems through inlining, value semantics, and data flow analysis.

We are confronted today with the increasing complexity of our C++ software systems. To manage this complexity and build larger applications and systems, C++ strives to form emergent structures (often found in nature, such as snowflakes' symmetrical structures), where simple patterns combine to form a remarkably complex and powerful system. These structures provide both a means to limit the complexity of each component and the essential economies of scale we rely on when developing software. From handheld devices to warehouse-sized data centers, motivated by smaller devices and increased concerns over power consumption, we are relying upon C++ to deliver these complex systems with unmatched efficiency. Our optimizing compilers today are more important than ever before and are utterly opaque to most practicing programmers. Compounding matters, the very emergent structures which allow C++ systems to scale for humans often provide unique and unsolved challenges to optimization. In this talk, I will start with a brief overview of how modern optimizing compilers work with C++ code at a very high level. I will then walk through the specific structures and patterns of C++ code, which are at the core of forming emergent structures out of simple, elegant elements. I will also address how these interactions can be effectively modeled and analyzed by a compiler to produce efficient final programs. All of this will be illustrated by a collection of real world case studies, which are broadly applicable and show up throughout modern C++ code bases. My goal is to give a framework for understanding these interactions both in the C++ code and the optimizing compiler, so that programmers are aware of the implications posed by these patterns. Finally, I will introduce a set of principles and techniques for designing and implementing C++ programs and libraries to specifically clear the way for modern optimizers while retaining the simplicity of each component and the power of the combined whole. --- *--* ---

Why it's here

2 people recommend this

What people say. Engineers recommend this talk for understanding how compilers optimize C++ code and the performance benefits of value semantics.

Moments people linked to

programming-talkscurated list on GitHubRecommends

Under “C++”

Optimizing the Emergent Structures of C++ 01:33:02 by **Chandler Carruth** (somewhat annoyingly bad quality) (2013)

humanrebaron Hacker News • Recommends

On “Why I don't spend time with Modern C++ anymore”

Here's a few: - "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!" link - "Understanding Compiler Optimization" link - "Optimizing the Emergent Structures of C++" link to this video - "Efficiency with Algorithms" link The first one is really good if you've never benchmarked anything in a Linux environment. Or if you otherwise want to learn how to investigate how C++ gets turned into machine code.

u/skeetoin r/C_Programming • Mentions

On “Is using pointers and dereferencing slower than using non-pointer variables?”

The performance hit isn't from the actual dereferencing, but rather that unnecessary references prevent important optimizations, especially loop vectorization. Pointers are often permitted to *alias* one another, and compilers must handle the situation even if you never intend for that to happen. This requires extra loads and stores, and creates false loop-carried dependencies. As a result, value semantics and copying can be a lot faster simply because it doesn't inhibit optimizations. Related talks: * Understanding Compiler Optimization * Optimizing the Emergent Structures of C++

u/skeetoin r/C_Programming • Mentions

On “Why is there no C conference?”

Every year you can think of a subset of CppCon as being like a C conference. There are a bunch of presentations that are entirely or mostly relevant to C, and not just in a broader sense of being relevant to *any* language. I plucked out a bunch from CppCon 2017 for you. These are *roughly* ordered from best to worst: * [Matt Godbolt “What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”] * Chandler Carruth “Going Nowhere Faster” * James McNellis “Everything You Ever Wanted to Know about DLLs” * John Regehr “Undefined Behavior in 2017 (part 1 of 2)” * John Regehr “Undefined Behavior in 2017 (part 2 of 2)” * Piotr Padlewski “Undefined Behaviour is awesome!” * [Tob…

u/skeetoin r/C_Programming • Mentions

On “dvector.h - public domain single-file vector math library”

Since this is a header library, all these functions are likely to be inlined, and so the argument passing semantics don't matter. If they're not being inlined, then you've got bigger issues. Besides, this is [a place where value semantics are desirable] as it eliminates aliasing issues.
4View on Reddit

mihai_ionicon Hacker News • Mentions

On “Linus Torvalds - C++ is a horrible language”

…cading everything between translation units and/or behind opaque pointers. While yes, LTO is supposed to help there, all those memory accesses make it infinitely harder for the compiler to prove trivial properties about the data flow. See also this recent presentation by someone who has a lot more experience than me with writing compiler backends: link to this video…

dottrapon Hacker News • Mentions

On “One year of C”

…sing. I wondered about a couple of things. What happens if the do_stuff_to_foo is actually defined (and what happens in that actual function)? And is there a difference between value semantics and pointer/reference semantics? These questions were my take-away from one of Chandler Carruth's C++ compiler optimization talks, I think it was this talk. link to this video My take aways were that the optimizer gets a huge chunk of its performance by inlining. And with value semantics, the optimizer can "cheat like crazy". So I defined two different variants for do_stuff_to_foo Original Pointer/Reference Semantics: void do_stuff_to_foo(foo* a) { a->x++; } Value Semantics: foo do_stuff_to_foo(foo a) { a.x++; re…

u/skeetoin r/C_Programming • Mentions

On “Best way to do vector math?”

In this situation I always choose the first version, and I'm careful to arrange my code such that it's very easy for the compiler to inline, such as by defining the function in a header. [The first presents more optimization opportunities for the compiler because of its emphasis on value semantics]. The pointers in the second version are a liability because the compiler must consider the case where the arguments point to the same vectors. This *can* be mitigated somewhat with `restrict`, but I'd rather just sidestep the whole issue. Inlining should be your main goal because this function is so small, and once it's inlined it's likely to be heavily transformed, perhaps even vectorized,source…

u/skeetoin r/C_Programming • Mentions

On “I made an STB style swiss table implementation”

> even passing a st_str by value makes little to no difference That's what I expect to be the case, and generally you shouldn't worry about the performance differences between these cases. I wasn't when I mentioned it originally. (I mentioned aliasing because the most common objection I get to this suggestion is about performance, which in reality only makes my suggestion stronger.) In my experience (and others), small structs that are semantically values or fat pointers are best passed/returned by copy *as a general rule*. They're nicer APIs, and if anything, performance is equal or better than passing by address. When it's better you get the benefits without thinking about it. > from t…
2View on Reddit