4

How do I determine what my compiler (g++) is doing with template code?

I am using boost.proto (an expression-template library) to evaluate some maths expressions at compile time. The code evaluates the expressions correctly, but I would like to see whether the compiler has expanded out the expression to the equivalent of hand-written c-code (i.e. eliminated all the temporaries), or whether there is still some further compile-time optimizations to be done.

Is there a way to see what the compiler has done with the templates?

Thanks

Tom
  • 5,219
  • 2
  • 29
  • 45
  • You can look at the assembly and compare it to the assembly of had-written C-code. – Björn Pollex Jun 30 '11 at 10:03
  • @Space_C0wb0y is there anything in between my code and assembly - I have never looked at assembly before and find this prospect a bit daunting? – Tom Jun 30 '11 at 10:08
  • @Tom: Nothing that I am aware of. – Björn Pollex Jun 30 '11 at 10:09
  • @Tom: No, there is no intermediate step. There is `g++ -E` to see what the preprocessor does, but templates are up to the compiler, and once the compiler is done, you got assembly... – DevSolar Jun 30 '11 at 10:11
  • You can get g++ to output the code after preprocessing with the flag -E but I dont know if the preprocessor will replace the templates for you. – Nobody moving away from SE Jun 30 '11 at 10:12
  • @Nobody, I think the template unwinding is done after the preprocessor – Tom Jun 30 '11 at 10:15
  • 1
    @Tom, there is a plenty of passes in between your source code and assembly. See all that `-fdump-...` options. – SK-logic Jun 30 '11 at 10:29
  • @SK-logic: I doubt that RTL representations of your code are any easier to comprehend than ASM... – DevSolar Jun 30 '11 at 10:55
  • @DevSolar, of course they are easier. Take a look at the `original` pass, for example - it is almost a pure C. – SK-logic Jun 30 '11 at 11:12
  • @Tom, another option for you: Elsa (oink) can dump a readable AST after a type propagation and templates instantiation pass. – SK-logic Jun 30 '11 at 11:13
  • @SK-logic Thanks I found the fdump useful, if you put your comment in an answer I will accept it (I was looking for an indication of what the compiler was doing and the fdump gives it to me). – Tom Jun 30 '11 at 11:42

2 Answers2

2
g++ -S

is documented as "Compile only; do not assemble or link". Basically you get assembly output.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • oh, I was hoping g++ went to c and then went to assembly. I have never looked at assembly before, maybe I need to learn... – Tom Jun 30 '11 at 10:10
  • @Tom There are a few rare C++ compilers which are really front-ends that emit C code which is in turn compiled; CFront, the very first C++ compiler, worked this way. But outside of Comeau and Edison, no one has done it that way for decades. G++ and MSVC compile directly from C++ to object code. – Crashworks Jun 30 '11 at 10:19
  • @Tom, you may also want to consider compiling your code with Clang instead of Gcc - it is much easier to inspect LLVM IR than an assembly and intermediate GCC representations. You can even produce a plain C code out of it, with LLVM CBE target. – SK-logic Jun 30 '11 at 10:31
  • @Tom: ...but none of these will really tell you how the *other* compiler does it, because how a compiler turns template source code into machine code is very much compiler-specific. So, to paraphrase Donald Knuth, don't bother with optimizations until you *know* you have a performance problem. 98% of the time, it simply *does not matter* if a piece of code works at maximum efficiency. – DevSolar Jun 30 '11 at 10:52
  • 1
    @DevSolar, template instantiation process is strictly defined by C++ standard. – SK-logic Jun 30 '11 at 11:14
  • @DevSolar, thats true, but in this case I am calling my expression 10-50 million times. Also, I am curios as to whether I am using proto library correctly (I want to show off to my friends...) – Tom Jun 30 '11 at 11:27
2

There are several ways to see a C++ code after the templates instantiation pass:

  • Use gcc -fdump-tree-original (or even -fdump-tree-all to see more passes)
  • Use Elsa C++ parser: http://scottmcpeak.com/elkhound/sources/elsa/
  • Use Clang and an LLVM C backend - the latter will give the most unreadable code, but it is still useful in some cases. There should be some AST dumping functionality in Clang itself as well.
SK-logic
  • 9,605
  • 1
  • 23
  • 35