Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should function attributes go?

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 */;
like image 841
KevinZ Avatar asked Feb 24 '20 17:02

KevinZ


People also ask

What are function attributes?

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.

Do functions have attributes?

Functions are objects. Everything is an object. And therefore it makes sense that functions have assignable attributes.

What is the use of __ attribute __ in C?

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.

Are function arguments attributes?

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.


1 Answers

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).

like image 55
eerorika Avatar answered Oct 13 '22 05:10

eerorika