When running gcc
with optimizations-on, it clones (duplicates) C functions when it considers that the function is in a hot path or there's constants propagating to the function arguments.
More specifically, this seems to be controlled by the fipa-cp-clone
option.
Is there any way to influence this? For instance mark one parameter with some attribute, as a compile-time constant (like you can do in C++ with a template parameter) which will cause the function to be cloned?
The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.
-O2 GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify. -O3 Full optimization as in -O2; also uses more aggressive automatic inlining of subprograms within a unit and attempts to vectorize loops.
Optimization level -Ofast -Ofast performs optimizations from level -O3 , including those optimizations performed with the -ffast-math armclang option. This level also performs other aggressive optimizations that might violate strict compliance with language standards.
The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization.
What matters is whether the function is called with a constant argument (either an actual constant expression, or something determined to be constant by the compiler via constant propagation). In this case, GCC will clone the function unless it determines doing so would be too costly or have too little benefit; I don't know a way to influence that metric. Be aware that constant propagation happens only within a single translation unit (source file) unless you're compiling the whole program at once or using link-time optimization, and I'm not sure whether cloning can still happen at that point or not.
My best guess, if cloning isn't happening when you expect that it should, is that GCC is never seeing a constant argument where the function is called. Even if you know it will be constant, the compiler might not be able to prove it is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With