cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubuIDE and RT-Thread Local variables are optimized out

asdss
Associate II

Dear experts
I use STM32CubeIDE to compile the RT Thread project. When the optimization level is selected as "Optimize", local variables will be optimized,
Only when "Optimize for debug" is selected, local variables will not be optimized and the program can run normally,

May I ask why this is happening and what should I do?
Using 'Optimize for debug' will result in losing a lot of flash

asdss_0-1725539283206.png

asdss_1-1725539474281.png

asdss_2-1725539494085.png

 

 

7 REPLIES 7
TDK
Guru

A variable being optimized out isn't a problem that needs to be addressed. This is expected behavior and you do not need to "correct" this.

If you want the variable to stay, but still want to compile with optimization, you can make the variable volatile.

volatile int index;

 

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

Thank you for your reply.
The local variable has been optimized, causing the program to run abnormally.
RT Thread is a third-party library with a lot of content; It still uses local variables in many places;
I added a volatile int index, and the index is normal, but there are still many others....
I compiled on Keil and used (- O3), and the program can run normally. Why doesn't work on STM32CubeIDE for "Optimize" ?
I don't think it should optimize my local variables.

asdss_0-1725540173497.png

 

TDK
Guru

> I don't think it should optimize my local variables.

But that's what compilers do. You can turn off optimization if you don't want that behavior.

Let's step back a bit: your program doesn't work with optimizations, you debug and step through and notice local variables are optimized out. This doesn't mean that local variables being optimized out is causing your problem. There is a software bug somewhere else that you need to find and address.

You should investigate what "run abnormally" means here in particular and debug the code to understand why that's happening. Local variables being optimized is not the problem.

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

@asdss wrote:

The local variable has been optimized, causing (sic) the program to run abnormally.


As @TDK said, that's a non-sequitur 

You have 2 observations:

  1. The local variable has been optimized-out;
  2. the program runs "abnormally"

It does not follow that (1) caused (2).

The "optimisation" could be simply that the loop is managed by a register - so there is no variable.

Or it could be that the loop has been "unwound" - so no counter is required.

What is the value of RT_Object_Info_Unknown ?

Andrew Neil
Evangelist III

@asdss wrote:

When the optimization level is selected as "Optimize",


There are also several other options:

AndrewNeil_0-1725545204335.png

For details of what they do, see:

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

 

 

 

Thank you for your reply.
You're right, optimizing local variables is not a problem.
I delved deeper into the comparison and found that the content pointed to by 'rt_oobject_comtainer' seems to be incorrect,
Please refer to the following figure. I compared the pointing values of rt_ort_comtainer under Debug and Optimize (O1) optimization levels

 

asdss_0-1725584469903.png

Under Optimize (O1) optimization level:
After obtaining the program information&rt_oobject_comtailer [2], it cannot exit the for loop, which will generate a hardware error interrupt approximately 5 times.

asdss_1-1725584679334.png

 

The content of the rt_oobject_comtailer linked list is generated to: I am not very familiar with what the compiler has done to cause the content of rt_oobject_comtailer to deviate.

asdss_2-1725584920618.png

Please help me, thank you

 

Thank you for your reply.
RT_Object_Info_Unknown = 3;
I have updated the reply information above, please help me check again, thank you.