cancel
Showing results for 
Search instead for 
Did you mean: 

How to change code optimization for a particular function/interrupt?

Fabian21
Associate III

Hi everyone,

I am working with some interrupt stuff and the interrupt needs to be fast. I need at least "-O2" optimization in gcc. However, setting the whole project to O2 is not a good solution as debugging becomes quite hard.

I found two possibilities to tell gcc a custom optimization setting for a particular function:

Add the optimize attribute:

void __attribute__ ((optimize("-O2"))) EXTI4_15_IRQHandler(void)
{
    // ...
}

Or use the pragma command:

#pragma GCC push_options
#pragma GCC optimize ("-O2")
void EXTI4_15_IRQHandler(void)
{
    // ...
}
#pragma GCC pop_options

Sadly both methods have no effect on the optimization.

I also tried this with a normal function, in case it is something special about the interrupt, but it also did not work.

Any suggestions?

8 REPLIES 8

> Sadly both methods have no effect on the optimization.

How do you know?

If you use Cube/HAL, it's likely that the bulk of the interrupt is *not* the EXTI4_15_IRQHandler() function, but whatever function is called from there. And then you may need to optimize that function (and all other functions called from there), too.

JW

Fabian21
Associate III

I am using CubeMX, but not the HAL, everything is using LL.

The startup assembler file directly defined EXTI4_15_IRQHandler() as a weak function and puts it in the interrupt vector table, so I guess there is no other code anywhere.

Additonally, as I said, I also tried it with a seperate function which contains a simple while(1) toggling a pin. The toggle frequency changes significantly when the optimization is tuned on globally, but it does not with either of the mentioned methods.

> The startup assembler file directly defined EXTI4_15_IRQHandler() as a weak function and puts it in the interrupt vector table, so I guess there is no other code anywhere.

And you are trying to optimize that weak function? I guess not. I guess you've written your EXTI4_15_IRQHandler().

Show the source and disasm for both optimized and non-optimized code.

JW

Fabian21
Associate III

I am not trying to optimize that weak function, as I wrote my own definition, which overrides the weak one.

Nevertheless, I think I figured it out myself. The "Low Level" driver is not as low as I thought it would be. The functions are not inlined and therefore not optimized. Thats why I don't see any (significant) difference. It worked in case of the while(1)-pin-toggle once I implemented the toggle myself directly in my own function without using calls to LL_GPIO_* but direct register access instead.

Thanks a lot anyway! But looking at the assembler was the right tip to bring me forward 🙂

Cartu38 OpenDev
Lead II

@Fabian2​ adding __attribute__ ((optimize("-O2"))) is working on my side based on STM32CubeIDE 1.6.0 at least

Fabian21
Associate III

@Cartu38 OpenDev​ Including other non-optimized, non-inlined functions?

Yes.

I've played with sysmem.c file adding extra optimization moving from 'void *_sbrk(ptrdiff_t incr)' to '__attribute__ ((optimize("-O2"))) void *_sbrk(ptrdiff_t incr)'

Works for me moving from 108b to 68b size contribution.

I don't see why other functions than the one with the flag should be optimized. It did not do that in my case.

But my problem is solved anyway by putting everything in the optimized function.