cancel
Showing results for 
Search instead for 
Did you mean: 

Dealing with compiler problems (over-optimizing to nonsense)

Linas L
Senior II

Hello.

Very frequently I end up chasing problems that not suppose to be in first place.

Here is example ( making 32b counter of 16b by counting overflows)

volatile void Delay_ms(uint32_t ms) // delay in ms
{
  time = 0;
   time_start = LPTIM2->CNT;
   overflow =  TIM2_IRQ;
  while(time<(ms*33))
  {
    over = TIM2_IRQ-overflow;
    over = over * 32768;
    time = over+LPTIM2->CNT-time_start;
  }
}

What is strange that all variabes are global volatile 32b unsigned integeres. And variable over just does not exist after compilation, so my code will hang if I ask more than 65535 counter tiks. (TIM2_IRQ is incremented by 1 inside reload interrupt elswhere each second)

Setting optimizing to zero solve this problem, but variable over is used and should not be optimized !

How do you deal with problem like this ?

(IAR ARM)

12 REPLIES 12

From partial snippets without context it's impossible to judge, what's going on.

Generally, compiler is free to optimize out accesses, where it does not see any consequence of the access. That means, anything which is not a plain memory, is supposed to be qualified volatile. In your example above, if SARA_DATA array is read or written by DMA, that's something the compiler does not know about, so it does not know that it may change without it being notified. That's why SARA_DATA ought to be qualified as volatile.

If you suspect the compiler is faulty, you should contact the compiler maker.

JW

Linas L
Senior II

I am starting to think I have some kind bad karma with STM32L5

0693W000005A2VQQA0.pngThis is working, I get how many transfers left, but this call is in main.c just as debugging purpose.

But if i create same exact type of global variable near function that does array cleaning, I get 0, while just before it was reading correctly. (in main)

This is what i get in function:

0693W000005A2WOQA0.pngAt this point, WTF ? How can I write any program that i need to check each step of this code. I spend so much time chasing bug like this and i am quite mad to be honest...

Since i am not assembler programmer, does any one understand what ASM code tell us ? why in first picture it does work, and why second does not ?

How can I use inline ASM to get around this problem ?

TDK
Guru

You keep saying there's a bug, but you haven't actually demonstrated that clearly anywhere, just snippets and statements without much context. The compilers are quite mature. It's unlikely there are glaring issues within them.

If you feel a post has answered your question, please click "Accept as Solution".