Suppose I want to mark a non-inline function with [[gnu::cold]];
should the attribute be placed in the declaration in the header, or should it go with the definition in a source file? Assume that I will not be using LTO and simply want that specific function to be optimized for binary size and not execution speed.
header example:
[[gnu::cold]] void rarely_called_func();
source file example:
[[gnu::cold]] void rarely_called_func() { ... }
Also, which position in the declaration/definition should it be:
/* A */ int /* B */ func () /* C */;
Function attributes are extensions implemented to enhance the portability of programs developed with GNU C. Specifiable attributes for functions provide explicit ways to help the compiler optimize function calls and to instruct it to check more aspects of the code.
Functions are objects. Everything is an object. And therefore it makes sense that functions have assignable attributes.
The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.
Some function attributes take one or more arguments that refer to the function's parameters by their positions within the function parameter list. Such attribute arguments are referred to as positional arguments.
Unless the attribute is seen by the compiler, it cannot use the attribute in its optimisation. If you don't put the attribute in the declaration, then the compiler cannot see the attribute. Conclusion: In order for the compiler to use the attribute for optimisation, you must put the attribute to the declaration of the function (in the header file).
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