"inline" attribute has no effect

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello

I want to use the "inline" or "__inline__" attribute to avoid the function-call overhead. But it dosen't work at all: When I look at the generated asm code, I can see that the function is not inlined. Has anybody an idea what I am doing wrong?

// => Function in "tools.c"
inline u16 ATOMIC16(volatile u16 *var)
//-------------------------------------------
// avoid ISR during access to the 2 bytes of a 
// volatile int, which might be modified in ISR
// => Atomic volatile access oustide of ISR
//-------------------------------------------
{
  u16 temp;
  u08 sreg_cpy;
  sreg_cpy=SREG; // save SREG
  cli();         // disable interrupts
  temp=*var;     // get value of volatile var
  SREG=sreg_cpy; // restore SREG
  return(temp);  // return the value
}

// => comiled code-fragment of main()
110: AVR_init();
  CALL    0x00001670       Call subroutine
111: CHECK_reset();
  CALL    0x000016AD       Call subroutine
113: temp=ATOMIC16(&WDR_timer);
  MOVW    R24,R28          Copy register pair
  ADIW    R24,0x01         Add immediate to word
  CALL    0x00000EB5       Call subroutine
115: ENS34_init();
  CALL    0x00000A2E       Call subroutine

I tried with acr-gcc 4.1.1 and avr-gcc 3.4.5, always same result (-O1 or -Os, AtMega128)

Thanks for any hint in advance!

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try tacking on "__attribute__((always_inline))" to the function prototype to force GCC to inline it.

I think perhaps the inlining is failing because the function is outside the current code unit. Because each unit (C file) is compiled separately and then linked together perhaps GCC is unable to inline it.

Does making the function static inline inside the same file have any effect?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I think perhaps the inlining is failing because the function
> is outside the current code unit.

Obviously, the compiler can only inline stuff which it is actually
seeing while compiling. In GCC 3.x, that means inline functions do
*only* make sense when they are completely local to the current
compilation unit, i.e. either inside the C file (declared before
used), or inside a header file. Only "static inline" makes sense
in that context.

In GCC 4.x, it could also work across compilation units provided
all compilation units are compiled at once. Note that the standard
Makefile templates don't work that way though, i.e. they feed each
C source file individually into the compiler rather than all
together.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are there any significant benefits to compiling all the units at once? Better optimization as a whole?

I'd be interested in testing that out (I'm using GCC 4.1). Do you have a sample makefile I could adapt to test?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> Are there any significant benefits to compiling all
> the units at once? Better optimization as a whole?

Yes, I believe it can optimize across all compilation units,
but didn't try it myself. If I RTFM correctly, you might have
to specify -funit-at-a-time unless your optimization level is
already -O2 where this is becoming the default.

Sorry, I don't have a Makefile template. In make's terms, it's
probably best to define the dependency of the final (linked) ELF
file from all C source files, followed by a command that does
all this in one step (so no intermediate .o files are kept).

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okydoky, thanks for the answers!

I've verified: In fact, the functions are inlined only if I put them local as "static inline"

So, if I want to use the function in many different source files, I have to put it into the header file.

I guess, the compiler will remove any unused static functions, if they are never called in the curent compilation unit.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I guess, the compiler will remove any unused static functions,
> if they are never called in the curent compilation unit.

It does that even for normal static functions, yes, and in particular
for "static inline" functions.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.