KDS: variable optimized out when compile with O2 and LTO option

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KDS: variable optimized out when compile with O2 and LTO option

2,317 Views
vaughn
Contributor II

Hi all. I have two questions.

In the startup.c file, the stack pointer declaration _SP_INIT as char[], when I add LTO option with linker, there's a warning: "warning: type of '__SP_INIT' does not match original declaration", I want to disable it, and not know how?

2018-05-30_012414.png

And the second one is that, when compiled with O2, I found a locale variable is optimized out which used in asm instructions. So, my question is that, how to avoid this in ALL of source files generated by Processor Export?

2018-05-30_012837.png

0 Kudos
12 Replies

1,789 Views
vaughn
Contributor II

could anybody give some suggestions?

0 Kudos

1,789 Views
BlackNight
NXP Employee
NXP Employee

I have been running into this for myself in the past too. I'll put together a more detailed response to you (should be able to provide that today).

But I don't see your second problem about that local variable optimized? Can you provide more details about this?

Thanks,

Erich

0 Kudos

1,789 Views
vaughn
Contributor II

Thanks Erich.

The local variable optimized away when I removed the __attribute__(optimze("O0"))

local variable optimzed away.png

As compare, I remove the comment, and it display as following

local variable kept with attribute O0 set.png

0 Kudos

1,789 Views
BlackNight
NXP Employee
NXP Employee

That's normal and expected with higher optimization settings. If you want to keep it, the usual way is to define the variable with 'volatile'.

0 Kudos

1,789 Views
vaughn
Contributor II

hi Erich, I found local variable will optimized away even if volatile added, and GCC's version is arm-none-eabi-gcc 4.9.3.

an usage as follow:

#define CpuCriticalVar()    uint8_t cpuSR

#define CpuEnterCritical()    \
  do {                        \
    __asm volatile (                    \
    "MRS   R0, PRIMASK\n\t"    \
    "CPSID I\n\t"            \
    "STRB R0, %[output]"    \
    : [output] "=m" (cpuSR) :: "r0");    \
  } while(0)

#define CpuExitCritical()    \
  do{                        \
    __asm volatile (                    \
    "ldrb r0, %[input]\n\t"    \
    "msr PRIMASK,r0;\n\t"    \
    ::[input] "m" (cpuSR) : "r0");    \
  } while(0)

0 Kudos

1,789 Views
BlackNight
NXP Employee
NXP Employee

you would have to mark the cpuSR as volatile.

0 Kudos

1,789 Views
vaughn
Contributor II

Erich, no effect even if volatile added, so you can try it with compile option -O2 and linked with LTO

optimized away even if volatile added.png

0 Kudos

1,789 Views
BlackNight
NXP Employee
NXP Employee

I have documented how I have been fixing that warning message caused by -flto in GNU Link Time Optimization finds non-matching Declarations | MCU on Eclipse 

I hope this is useful,

Erich

0 Kudos

1,789 Views
vaughn
Contributor II

Erich, that doc helpful, thanks.

And the second question is that, are there bugs similar with optimized away within other modules generated by PE, e.g. UART, MSCAN, SPI. For I found another problem using UART, which runing ok with O0 compile option, and run to UnhandledInterrupt when UART transmit.

0 Kudos

1,789 Views
vaughn
Contributor II

when compiled with O2 and LTO, when run to UART_PDD_EnableInterrupt and continue, it run to UnhandledInterrupt, but i can not location the problem accuratly.

pastedImage_1.png

pastedImage_2.png

the UART source files attached.

0 Kudos

1,789 Views
BlackNight
NXP Employee
NXP Employee

To know which interrupt is causing this, turn on 'one for every', see Oh my! An Interrupt… | MCU on Eclipse 

I hope this helps,

Erich

1,789 Views
vaughn
Contributor II

Thanks Erich, that's great.

0 Kudos