How do you know if a function is inlined? (C++)

Started by
11 comments, last by BigJim 15 years, 2 months ago
Does anyone know of a way to test if a function call is being inlined or not? Basically, Im trying to determine whether GCC's whole program optimization is as good as I think it is. I had gotten into the habit of never explicitly inlining any functions, because the compiler is free to ignore the directive and it usually does a pretty good job on its own. However, recently, Ive been running some benchmarks to see if explicitly inlining functions makes any difference and Ive been getting confusing results. In particular, it seems like GCC is incapable of inlining template functions that are instantiated externally, but I cant say for sure. (Im not sure if this is the correct terminology, but it's the last compilation method mentioned in this C++ faq). If you have any suggestions about the value of explicitly inlining, or have any knowledge about GCC's whole program optimization in particular, I would appreciate the advice.
Advertisement
Quote:Original post by WhatsUnderThere
Does anyone know of a way to test if a function call is being inlined or not?

Read the generated assembly. The gnu compilers provide the -S option for that.
I didn't know the answer to your question, but I knew how to find out. (DevFred's solution works for any compiler, but you can make gcc tell you explicitly.)

Go to your command line and type "gcc --help" (g++ works as well, but I wanted to point out it works for gcc, too, and you get the same results). You'll get a list of arguments gcc (or ++) can work with. The one of note is --help={...}. You can replace the {} with any of the words in it separated by the OR things. (This is regex for one of.) We want gcc to warn us when a function was not inlined. Thus, we want the option waringins.

Type "gcc --help=warnings". You'll get a lot of text. Too much for you to want to read all of, right? And the command line isn't searchable. So, let's do that command again, only redirect the output to a text file. 'gcc --help=warnings > "a text file.txt"'. Now, you can go into that text file with a text editor, search it for inline, and see what comes up. The option you're looking for at least tells you when a function could not be inlined, but to see what WILL be inlined, you'll have to look at the assembly.

But, if you look through --help=optimizers and search for inlin (excluding the e makes inlinING show in the search), you might get a better picture of WHEN g++ inlines functions.

Unfortunately, as you'll find, there is no easy way to really find out what gets inlined and why, but this is a start.
Thanks for the suggestions. I had actually tried looking at the assembly output already--unfortunately, Im not at all familiar with assembly language. I was trying to look for repeated snippets to see if the function was being called in-place, but it was way over my head. I suppose this is as good a time as any to learn some assembly, but I would prefer to avoid the detour if there is a simpler way to figure this out.


Also, I saw something interesting when browsing the help file. GCC says that -O3 is the only method that uses inline optimizations. Does this mean functions I explicitly make inline will only be inlined with -O3 on, or does it mean GCC will start inlining additional functions that were not declared inline? Ive spent a good amount of time googling and looking through documentation, but Im still finding the way inlining works a little counter-intuitive.
Quote:Original post by WhatsUnderThere
Thanks for the suggestions. I had actually tried looking at the assembly output already--unfortunately, Im not at all familiar with assembly language. I was trying to look for repeated snippets to see if the function was being called in-place, but it was way over my head. I suppose this is as good a time as any to learn some assembly, but I would prefer to avoid the detour if there is a simpler way to figure this out.

Just look for the call instruction. If it's not there, you've got inlining.
Quote:
Just look for the call instruction. If it's not there, you've got inlining.


That's true, unless the inlined function calls a non-inlined function... which may not be intuitive to someone who doesn't know assembler.
Quote:Basically, Im trying to determine whether GCC's whole program optimization is as good as I think it is.

GCC doesn't do whole program optimization.

-O3 is a short option for lots of other options. It, among others, enable aggressive inlining.
Inlining will be performed without that option, of course, just less aggressively.
Quote:Original post by loufoque
Quote:Basically, Im trying to determine whether GCC's whole program optimization is as good as I think it is.

GCC doesn't do whole program optimization.

-O3 is a short option for lots of other options. It, among others, enable aggressive inlining.
Inlining will be performed without that option, of course, just less aggressively.


http://gcc.gnu.org/wiki/whopr

(No , its not finished, but it might be worth watching as it progresses)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by WhatsUnderThere
Thanks for the suggestions. I had actually tried looking at the assembly output already--unfortunately, Im not at all familiar with assembly language. I was trying to look for repeated snippets to see if the function was being called in-place, but it was way over my head.


With gcc, you can do an inline assembly comment to "mark" a spot in the file. In AT&T syntax, the # makes a comment, so the fallowing line can be used to mark a spot in the source:
    asm("# f(x) STARTS HERE");
Actually, I do this a lot just to find where in my source ANYTHING happens! But, inline asm forces the compiler to do exactly what you say (that's the point!), so this might theoretically mess with out-of-order operation optimizations(?).

As mentioned, you can alternatively look for the call instruction. You can try finding your function as a label too. I don't understand what it all means, but the fallowing is a function in assembly:
    #C++: void my_function()    .globl __Z11my_functionv        .def	__Z11my_functionv;	.scl	2;	.type	32;	.endef    __Z11my_functionv:
Of coarse, I added the comment noting the signature, but the rest is pure gcc produced asm. __Z seems to be the prefix they use for a function, don't know what 11 means, my_function is written out plain, and the v on the end is obviously the argument type: void.
Quote:Original post by loufoque
Quote:Basically, Im trying to determine whether GCC's whole program optimization is as good as I think it is.

GCC doesn't do whole program optimization.

-O3 is a short option for lots of other options. It, among others, enable aggressive inlining.
Inlining will be performed without that option, of course, just less aggressively.

Just to be clear then, will -O3 allow GCC to inline functions that are defined in separate translation units?

If it simply inlines functions from the same source file (that werent marked inline), that would be a pretty insignificant optimization in most cases, wouldnt it?

This topic is closed to new replies.

Advertisement