Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing inline with a single macro in GCC, Clang and Intel Compiler?

I have a function that I need inlined in a tight loop in C++11

I want the function to be implemented in a separate file from the header and still force the it to be inlined everywhere it is used. Also, I want to compile with both clang, GCC and the Intel compiler.

To flesh out the requirement. I am looking for a macro that would allow me to do something like:

#define force_inline <something here>

In headers:

force_inline void foo();

And I should be able to do this in the implementation file:

void foo() {... Code.. }

Just to be clear, I do not want to put code in my headers. I want them to only contain the declaration of the function.

Is there a way to achieving inlining with a macro that works on all those compilers?

The best solution I have so far is this macro:

#define forceinline inline __attribute__((always-inline))

It seems that ICC needs both inline (which has nothing to do with inlining the code) and the full implementation in the header to guarantee inlining of the function.

PS: Yes, I have measured my performance and I know for a fact that it is faster to have the function inlined than not. And no, the compiler does not do it for me.

like image 739
Thomas Kejser Avatar asked Nov 24 '14 18:11

Thomas Kejser


1 Answers

By definition, an inline function has its code included (by the compiler) at each place it's called.

It means the compiler needs to be able to access the function's code when it builds the callers compilation unit. This is technically feasible, but not easy and hardly scalable. Note that if you distribute only your header and a library, it means the compiler using your component has to retrieve the code from the library !!

If implemented by a compiler, it means that you won't benefit from having code out of header as much as for regular functions (changing the CPP file will require recompiling all the files depending on the header, even if it didn't change). The only benefit would be to have a header that doesn't contain code (which is still a good thing).

The only currently available solution that I'm aware of is the link time optimization of GCC, therefore not meeting your requirement of working with clang and icc: https://gcc.gnu.org/wiki/LinkTimeOptimization Maybe something similar exists for those compiler with specific options, which would need to do compiler dependant code to support it.

Solution 1: You could keep a "clean class" without any implementation in the header and put the inline functions outside of the class (but still inside the header). You still have definitions in the header but it's clearly separated from the declaration so it hurts less the readability. (This is my prefered solution personally)

Solution 2: Another way to mitigate your issue, if you really want only the declarations in the header, is to separate your code in 3 files instead of 2: - a .h file containing only the interface - a .i file containing inline functions and included by the .h file - a .cpp/.cc file containing the rest of the code

Obviously this has drawbacks too as your code is now separated in two different files ...

Please tell me if you see an issue that I missed and that you see in these propositions.

like image 150
Colin Pitrat Avatar answered Oct 21 '22 15:10

Colin Pitrat