How much overhead can the -fPIC flag add?
As other people already pointed out -fPIC
forces GCC to disable many optimizations e.g. inlining and cloning. I'd like to point out several ways to overcome this:
- replace
-fPIC
with-fPIE
if you are compiling main executable (not libraries) as this allows compiler to assume that interposition is not possible; - use
-fvisibility=hidden
and__attribute__((visibility("default")))
to export only necessary functions from the library and hide the rest; this would allow GCC to optimize hidden functions more aggressively; - use private symbol aliases (
__attribute__((alias ("__f")));
) to refer to library functions from within the library; this would again untie GCC's hands - previous suggestion can be automated with
-fno-semantic-interposition
flag that was added in recent GCC versions
It's interesting to note that Clang is different from GCC as it allows all optimizations by default regardless of -fPIC
(can be overridden with -fsemantic-interposition
to obtain GCC-like behavior).
As others have discussed in the comment section of your opening post, compiling with -flto
should help to reduce the difference in run-times you are seeing for this particular case, since the link time optimisations of gcc will likely figure out that it's actually ok to inline a couple of functions ;)
In general, link time optimisations could lead to massive reductions in code size (~6%) link to paper on link time optimisations in gold, and thus run time as well (more of your program fits in the cache). Also note that -fPIC
is mostly viewed as a feature that enables tighter security and is always enabled in android. This question on SO briefly discusses as well. Also, just to let you know, -fpic
is the faster version of -fPIC
, so if you must use -fPIC
try -fpic
instead - link to gcc docs. For x86 it might not make a difference, but you need to check this for yourself/ask on gcc-help.
It turns out that when you compile without the -fPIC
option multiplyComplex
, sqComplex
, isInSet
and isMandelbrot
are inlined automatically by the compiler. If you define those functions as static you will likely get the same performance when compiling with -fPIC
because the compiler will be free to perform inlining.
The reason why the compiler is unable to automatically inline the helper functions has to do with symbol interposition. Position independent code is required to access all global data indirectly, i.e. through the global offset table. The very same constraint applies to function calls, which have to go through the procedure linkage table. Since a symbol might get interposed by another one at runtime (see LD_PRELOAD
), the compiler cannot simply assume that it is safe to inline a function with global visibility.
The very same assumption can be made if you compile without -fPIC
, i.e. the compiler can safely assume that a global symbol defined in the executable cannot be interposed because the lookup scope begins with the executable itself which is then followed by all other libraries, including the preloaded ones.
For a more thorough understanding have a look at the following paper.