• AVR Freaks

how to inline "C function" into other "C function"

Author
Dricdric
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2011/01/17 06:38:22
  • Location: 0
  • Status: offline
2011/01/17 06:48:22 (permalink)
0

how to inline "C function" into other "C function"

I try to inline "C function" into other "C function".
My project has a lot of functions in multiple files to be more readable.
Most of function are called one times. For example, an ISR is composed by a few function that are called just in the ISR.

When I compil and link, all function are called (rcall), but I want the function to be inlined because the code is very bad (a lot of call of very short code)

I see Help of C30.

I use :
__inline__ before void function,
__attribute((always_inline)) in the prototype declaration in .h files (that include all prototype for C files)
C30 attributes : -finline-function
==>but the code is not change, there is no warning or error

I do not find other information or examples in C30 help.

Is there a step that I miss?

#1

13 Replies Related Threads

    DavidePizzolato
    Junior Member
    • Total Posts : 95
    • Reward points : 0
    • Joined: 2010/11/25 11:55:54
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 07:23:52 (permalink)
    0
    from the C30 user guide:
     
    "Function inlining will only take place when the function’s definition is visible
    (not just the prototype). In order to have a function inlined into more than
    one source file, the function definition may be placed into a header file that
    is included by each of the source files."
     
    #2
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 07:35:23 (permalink)
    0
    "Function inlining will only take place when the function’s definition is visible
    (not just the prototype).
    I read it several times, and I think I do it?
    In my C sources files, I put :
    __inline__ void test(void) { -----code----- }
    Is that "visible" means something else?

    In order to have a function inlined into more than one source file, the function definition may be placed into a header file that is included by each of the source files.
    Each of C sources files of the project include the same .h files that have all prototypes of the project like:
    extern __inline__ void __attribute__((always_inline)) test(void);
    #3
    DavidePizzolato
    Junior Member
    • Total Posts : 95
    • Reward points : 0
    • Joined: 2010/11/25 11:55:54
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 07:47:05 (permalink)
    0
    Dricdric
    Each of C sources files of the project include the same .h files that have all prototypes of the project like:
    extern __inline__ void __attribute__((always_inline)) test(void);

    this is the point; the declaration is not enough, you must write the definition in the .h; or a better alternative is to write the function in the same file where it will be used, before its usage.
    #4
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 07:58:15 (permalink)
    0
    I don't understand, but I try to explain my test in others words:
    - I try to put a test function in the same C source files, with prototype in .h. I have the same problem. I Think it is the 2nd solution proposed but I can't. (I tried before asking the post to understand the mechanism, but in fact, I did not come either). Again, this only works if there is only one function called in one place 
    - For the 1st point, I'm lost (ambiguous for me)! : what is write the definition in .h ? Are we agree that the C source is not a macro, but a C source files that have global variables, etc..., witch include specific .h and definition?

    Maybe you want to tell me a specific things, but I do not understand what to do otherwise
    #5
    DavidePizzolato
    Junior Member
    • Total Posts : 95
    • Reward points : 0
    • Joined: 2010/11/25 11:55:54
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 08:14:39 (permalink)
    0
    in the next example the inline is working:
     
    // ------ file main.c ------
    inline short my_sum(short a, short b)
    {
     return a+b;
    }

    inline short my_test(short a, short b)
    {
     return my_sum(a,b);
    }
    ...
    // use my_test or my_sum
    //-----------------------------
     
    in the next example the inline will not work:
     
     // ------ file my_sum.c ------
    inline short my_sum(short a, short b)
    {
     return a+b;
    }
     // ------ file main.c ------
    inline short my_sum(short a, short b);
    inline short my_test(short a, short b)
    {
     return my_sum(a,b);
    }
    ...
    //----------------------
    #6
    cawilkie
    Administrator
    • Total Posts : 1977
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 08:15:07 (permalink)
    0
    Hint:  inlining is an optmization.  Try -O2.

    Regards
    Calum
    #7
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 08:39:06 (permalink)
    0
    I answer for the 2 previous post:

    - O2 optimization. I try optimization before for testing. I have the same result. The optimization is not needed for inlining (like me), but the optimization is required for specific inlining like -finline-limit=n  C30 attribute.
    In my case, I use -finline-function (like C30 help) for function inlining

    -OK for example. I understand the same things like you, but it doesn't work. When I see asm code, there is a rcall/return, then there is no inlining.
    Have you configure the C30 to have sucess result with -finline?
    In your example, there is no .h files, isn't it?
    #8
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 10:23:12 (permalink)
    0
    Where can I find "inline" project example.
    I try a lot of things, but I can not solve this problem
    It seems to be OK at compiling/linking (no errors, no warning), but when I see the asm code, I see that there is no inlining, I have always rcall and return instruction

    Actually, I share (short) function with ISR function and main function, then It is necessary for me to use inline with a common C source files that can be used at compiling by other C sources files, to avoid crash. A Header .h contains prototype for all functions.
    This way is common (on other compiler), then I sure that we do it on C30, otherwise...
    #9
    DavidePizzolato
    Junior Member
    • Total Posts : 95
    • Reward points : 0
    • Joined: 2010/11/25 11:55:54
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 10:30:29 (permalink)
    0
    Have you configure the C30 to have sucess result with -finline?
    In your example, there is no .h files, isn't it?

    the project is configured with optimization -Os
    the is no .h file in my example, but if you want to inline a function it is not necessary: the function body MUST be before any reference to that function; it should be the only way for the compiler to work with inlining.
    #10
    cawilkie
    Administrator
    • Total Posts : 1977
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 11:49:31 (permalink)
    0
    Also,  just because you say inline it doesn't mean the compiler will inline it.  There are many reasons why the compiler will not inline a function.  Its too big is one of them.

    -Winline might tell you why a function is not being inlined.

    Regards
    Calum
    #11
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 12:20:17 (permalink)
    0
    After intensive test, I think I find the problem.
    For testing, I see the disassembly listing and Program memory.

    In C30 attribute, -finline-functions do nothing. If I use -O -finline-functions, then, I see the inlining !!In the help, this one is not mentionned

    Then, I see that function is inlined, but the function exist in memory space, but never used.
    Then, I add to my definition the term "static" : __inline__ static short __attribute__((always_inline)) test(void)
    At this time, the function is removed from memory space, and is inlined just 1 times in the code ==> it works well.

    Then, my test is that :
    For inlining a function, you must:
    - C30 attribute : -O -finline -functions ==> -O is needed
    - the prefixe function definition is "__inline__ static" and not just "__inline__"
    - -Winline is usefull to see warning, but is not essential

    Note : a trap to avoid:
    - in disassembly listing, adress are not in order, and you must use program memory to see adress of jump !!!

    Now, I test with multiple c sources and common header....to see if it works again!

    #12
    cawilkie
    Administrator
    • Total Posts : 1977
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/17 13:13:17 (permalink)
    0
    I'm glad you got the hint,  :).

    -O2 is a collection of optimization settings, which includes -finline.  In order for -finline to work, optimization must be performed.

    Regards
    Calum
    #13
    Dricdric
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/01/17 06:38:22
    • Location: 0
    • Status: offline
    Re:how to inline "C function" into other "C function" 2011/01/18 03:25:49 (permalink)
    0
    I complete my precedent post.
    -O2 optimization is not for inlining. the C30 help tell that:

    -O2
    FULL VERSION ONLY
    Optimize even more. The compiler performs nearly all supported optimizations that do not involve a space-speed trade-off. -O2 turns on all optional optimizations except for loop unrolling (-funroll-loops), function inlining (-finline-functions), and strict aliasing optimizations (-fstrict-aliasing). It also turns on force copy of memory operands (-fforce-mem) and Frame Pointer elimination (-fomit-frame-pointer). As compared to -O, this option increases both compilation time and the performance of the generated code.

    The inlining is perform by -O (just -O) to authorize simple optimization that specified like -finline. I make a lot of test, and it is the only way that success inlining is the used of -finline-functions with static attribute.
    -O2 is for adding another optimization, but not inlining.

    To lead a inlining result, I must used inline with static.
    to have the inlining with several C sources files, I put the C function into a .h files, without "extern", and I not declared it into another .h files. I include specific .h function file into C source file that need inlining function.
    I would have preferred do it in C source files, but it seems that the C30 does not because the inling is processed at compiling step and not linking step (like other tools).

    For my part, I finish this post.
    Thank you for the support you gave me.






    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5