5

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?

Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
  • In addition to my answer, I'd like to comment on the question that I think you could get better answers if you posted a minimal example where you expected GCC to clone the function and it didn't. Then answers could address the specific issues causing it not to be cloned rather than having to guess what might be happening. – R.. GitHub STOP HELPING ICE Mar 16 '13 at 03:07
  • 1
    You can influence GCC to *not* clone a function using [`__attribute__((noclone))`](https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes). – Jonathon Reinhart Aug 09 '18 at 18:33

1 Answers1

6

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711