Russell Hadley: The Route to C++ Code Optimization

Download this episode

Download Video

Description

It's nice to write clean code (code that looks good, is organized, is easy for others to understand by reading it, etc). As developers we get to use great tools to implement algorithms in our favorite languages. The act of composing a program is much like that of writing a story or, in some cases, a poem Smiley But the underlying hardware isn't much interested in intelligent class hierachies and easy-to-understand lines of programming language syntax. Processors do not speak C++ or Java or C# or VB, etc.

The focus of this interview is mapping the (long and complicated)path to executable machine code that the machine natively understands and acts upon, bringing your code to life. How does this work, exactly?

Russell Hadley is a senior developer on the C++ team here at Microsoft and he spends his days (and nights, ocassionally) writing code that takes the front-end compilation linear (flattened) blob and turns it into highly optimized machine code patterns that the processor can execute in a highly efficient manner.

This is a deep interview with lots of whiteboarding, but it is shallow enough so you won't drown if you can't swim very well. Enjoy. This is another great conversation with one of the C++ experts who live in Building 41.

LOW RES FILE
MP4 FILE

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      obrienslalom
      I was thinking during that video how depressing it was that my job will never be as cool as his.  I felt like at the end you just wanted to stick it to me Tongue Out.
    • User profile image
      OliverTaylor

      Excellent interview as always, it's great to see more of the C++ team.

      I thought Charles read my mind here, as I was just thinking that it would be great to have some videos covering optimization.

      If there were any more videos on this theme I would be interested to know about what C++ code is difficult to optimize and how we should best write C++ to give the optimizer the most help we can about the semantics of our code, perhaps covering __assume et al?

      Thanks for another great In-Deep look into C++

    • User profile image
      BSalita
      Is the flow through phases always down? I'm thinking flow would be kicked back up because of failed optimizations, re-optimization opportunities, resource overflow, hardware target feedback, etc?

      Excluding cache factors, at what point can you know how many actual cycles or microseconds an operation takes? Is predicted performance always accurate? If not, what situations foul up predictions? Are there situations where performance is non-determinable?


    • User profile image
      berni
      Thank you for this great interview, i love watching this stuff. It would be interessting when microsoft starts using the new compiler on real world products like windows, office or the .net framework. Also it would be interessting how much faster the new code will run compared to the old code.
    • User profile image
      rhadley

      Hi!

      You bring up a good point.  Because we generate code with this sequential translation approach there are times when some optimization transformations expose new opportunities.  In general we call this the "Phase Ordering Problem".  The traditional approach (what we do) is to re-run the transformations that are profitable when there's compiler throughput budget to do it. (of course we can't iterate forever in addition some of the problems won’t converge)

      For the predicted cycles (and code size) of a particular instruction we typically start getting a rough idea at lower time when we select a machine opcode.  This becomes more concrete through register allocation and becomes very concrete in compiler terms at encoding time.  The compiler selected instruction has a processor defined cost in terms of machine resources (e.g. execution elements or slots in the out of order buffer) which are fixed but then data dependencies – is the input value available at the right time or schedule - and micro-architectural issues intrude.  Finally as you say the cache trumps the other issues.  I think it’s important to note here that we’re getting new machines all the time with different micro-architectures, in fact much more quickly than new compilers, and the compiler needs to try and make a single executable that gives good performance across a spectrum of machines.  So some of the instruction performance characteristics are hard to know ahead of time.  Of course we work closely with our partners to make sure that future machines provide good performance for Microsoft apps and tools output.  Finally, with respect to predictability, if you know the machine, it’s micro-arch, the working set, and execution environment (OS and workload) theoretically you can predict exactly the performance (digital computer after all) but in actuality we model a “typical” case, maintain good engineering, and then do lots of benchmarking on real world scenarios to ensure the performance of our output code.

    • User profile image
      Sniffy
      It would be nice if the interviewer was an actual C++ programmer.  The interview is at too high a level for me, I've known that the compiler produces .obj's that get linked by the linker for quite awhile now.
    • User profile image
      Charles
      Sniffy wrote:
      It would be nice if the interviewer was an actual C++ programmer.  The interview is at too high a level for me, I've known that the compiler produces .obj's that get linked by the linker for quite awhile now.


      That's a good point. This is precisely why we created the Expert to Expert series. In this case, we wanted to reach as broad of a programmer audience as possible since C++ is only one programming language that gets compiled (the information here is not just relevant to C++ after all - note the conversation on what the .NET JIT does, etc).

      I'm a generalist and we feel this is the best way to have deep, though broadly interesting, technical conversations.

      You will see a C++ Expert to Expert interview on C9 in addition to the more broadly-scoped conversations.

      Thanks,
      C
    • User profile image
      evildictait​or
      Charles wrote:
      
      You will see a C++ Expert to Expert interview on C9 in addition to the more broadly-scoped conversations.


      Yay! This was an awesome interview btw, and I'd definitely like to see more of these going deep interviews in the near future.

      Cheers
    • User profile image
      dancmarines​cu
      the wole interview is "technically" offensive (we're not all a bunch of goofy/ms certified end users)

      they called by "front end developer" the actual programmer (writing 3rd generation source code) ?!?

      i believe that interviews like this one are not mean to help microsoft's image in any way (poor backend r&d guy would almost lough at the questions, poor guy... Smiley

      anyway, you do what you gotta do, and we'll do what we gotta do 2!

    • User profile image
      Charles
      dancmarinescu wrote:
      the wole interview is "technically" offensive (we're not all a bunch of goofy/ms certified end users)

      they called by "front end developer" the actual programmer (writing 3rd generation source code) ?!?

      i believe that interviews like this one are not mean to help microsoft's image in any way (poor backend r&d guy would almost lough at the questions, poor guy... Smiley

      anyway, you do what you gotta do, and we'll do what we gotta do 2!



      ???

      Whatever....
      C
    • User profile image
      gndnet
      cool !

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.