0

Functions attributes can be specified only for function declaration (not definition). So, I can't specify attributes for nested function. For example:

//invalid line. hot_nested_function is invisible outside "some_function"
void hot_nested_function() __attribute__ ((hot));

//valid attribute for declaration
int some_function() __attribute__ ((hot));

int some_function(){
    void hot_nested_function(){
         //time critical code
    }
    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}

In such case, will be "hot_nested_function" optimized as "hot"?

UPD: On a dumb example, gcc (means gcc -O1 and higher optimisation level) replaces function calls by its body, for both of with and without __attribute__ ((hot)) (for nested function). Even nothing reminds about nested function.

UPD2: According to gcc.git/gcc/tree-nested.c, it resolves parent function references, external label jumps and so on. At next stage nested functions transforms to independent functions with inlining ability. But it is still unclear about parent function's attributes. Did they applying to nested?

1 Answers1

1
int some_function(){
    void __attribute__ ((hot)) hot_nested_function(){
         //time critical code
    }
    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}

gcc's function attribute syntax is one of

__attribute__ ((...)) returntype functionname(...)
returntype __attribute__ ((...)) functionname(...)
returntype functionname(...) __attribute__ ((...))

and last one could only be used in prototype, but not the first two.

Another way is

int some_function(){
    auto void hot_nested_function() __attribute__ ((hot));

    void hot_nested_function(){
         //time critical code
    }

    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}

As for attributes to be automatically applied to all containing objects - I do not know, and documentation says nothing about it, so it is up to compiler to decide. It is better be specified manually - behaviour may change between compiler versions.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • Please add explanation too so that others having same problems or reading for learning purpose can understand the change. Thank you. – Aduait Pokhriyal Mar 12 '14 at 10:27
  • Thank you, probably I miss this part of documentation. But what about second part of question? Did have __ attribute __ for 'some_function' any affection on nested function? – Alexander Sannikov Mar 12 '14 at 10:40
  • @AlexanderSannikov sorry, don't know that. Updated answer with a bit more info, however. It could be tested by looking to disassembly - but again, what will happen in different gcc versions. – keltar Mar 12 '14 at 10:52
  • @keltar, anyway, thank you a lot. I will try to disassemble different versions. – Alexander Sannikov Mar 12 '14 at 11:49