Page 1 of 3

LTO linker option

Posted: Tue Mar 30, 2021 6:18 pm
by vlad
Hi there
I'm using STM32H7 on Daisy Seed board with Windows Arduino IDE and normal compiler options:

U(S)ART support: "Enable (generic 'Serial')"
USB support (if available): "CDC (generic 'Serial' supersede U(S)ART)"
USB speed (if available): "Low/Full Speed"
Optimize: "Fastest (-O3)"
C Runtime Library: "Newlib Nano + Float Printf"
Upload method: "STM32CubeProgrammer (DFU)",

except by the Float Printf support that I need in my project. As the code became larger, it didn't fit in flash anymore. So I began to optimize the code size with linker -Ox and LTO options. However, after uploading the program on Daisy Seed with any "(-Ox) with LTO" options the COM port was not available for board communication. Even a simple code like this

int val = 0;
void setup() {
Serial.begin(9600);
delay(1000);
}

void loop() {
delay(1000);
val++;
Serial.println(val);
}

does not work with LTO options. Since this is my first contact with STM boards, I wonder if this should be the expected behavior of the LTO?

Re: LTO linker option

Posted: Tue Mar 30, 2021 8:51 pm
by mrburnette
LTO options were removed 3 years ago in the LibMaple (Roger's) core:

https://stm32duinoforum.com/forum/viewt ... _4377.html

I have no info about the STM32duino Official core.


Ray

Re: LTO linker option

Posted: Tue Mar 30, 2021 9:20 pm
by ag123
i think it is still possible to use LTO with STM core, i think its main use is to shrink the code size
but accordingly (earlier) LTO seem to be removing things that it deem you don't need, even if that crashes the app ;)
https://www.google.com/search?q=arm+gcc ... erflow.com

for optimization try -Os (smallest)
-O3 would likely simply create 'superfluous' codes that isn't suitable for micro-controllers, try imagine that
you have a code like

Code: Select all

int sum=0;
for(int i=0;i<1000;i++) sum+=i;
Serial.println(sum);
-O3 when seeing that you have a superscalar processor may just unroll that for loop

Code: Select all

int sum=0, i=0;
sum+=i++;
sum+=i++;
sum+=i++;
sum+=i++;
... repeat a thousand times
Serial.println(sum);
all that unrolled codes just become your binary

Re: LTO linker option

Posted: Wed Mar 31, 2021 7:10 am
by fpiSTM
From my point of view LTO is proposed "as it" but in several case it does not works as expected. They are several opened issues on gcc related to it.
So I always consider it as experimental.

Re: LTO linker option

Posted: Wed Mar 31, 2021 3:04 pm
by vlad
Thank you guys. Very helpful. Even with -Os I'm still exceeding 19kB of flash. I'll try to figure out in code what could be using so much memory. Things started to getting bad when I included code to process user input commands coming from serial line for audio effects control.

Re: LTO linker option

Posted: Wed Mar 31, 2021 3:39 pm
by ag123
stm32h7 should have plenty of flash isn't it? some bulk can't be avoided if you have things like usb etc compiled in (e.g. usb-serial). for data don't put that in the built-in flash, spi flash are moderately low cost and if you go the distance and use an sd card, that become gigabytes of storage.

Re: LTO linker option

Posted: Wed Mar 31, 2021 6:48 pm
by mlundin
Yes H7 boards typically has some MB or several of flash. So 19 kB is nowhere a flash limit.

So what board settings are you using?

The Daisy is not officially supported and they dont even tell what processor they use in their marketing, but a STM32 at 480 MHz sounds like STMH7

Re: LTO linker option

Posted: Wed Mar 31, 2021 7:24 pm
by fpiSTM
Daisy seed is supported. It is a STM32H750IBKx.
https://github.com/stm32duino/Arduino_C ... .txt#L1650

19k is very low compare to the 128k available. So you have an other issue.

Re: LTO linker option

Posted: Thu Apr 01, 2021 3:11 pm
by vlad
According to ElectroSmith, Daisy Seed uses a STM32H750, with 128KB flash, 480MHz. I'm not storing to much data on code, but yes, there are some string data to help user communication. I'm just trying to manually optimize the code, but as far as I can go is 1 or 2KB, still far away from the 19K. Since I'm using Arduino IDE, I adopted the recommended board configuration, but with "-Os" compiler option to shrink the code. So the options I have are to add external flash support for data storage (but it still requires code to read the data, so the expected gain shouldn't be so high), or to change from Arduino IDE to CubeProgrammer, to avoid usb support which seems to be one of the problems. Suggestions are welcome.

Re: LTO linker option

Posted: Thu Apr 01, 2021 5:17 pm
by mrburnette
vlad wrote: Thu Apr 01, 2021 3:11 pm ... but as far as I can go is 1 or 2KB, still far away from the 19K. Since I'm using Arduino IDE, I adopted the recommended board configuration, but with "-Os" compiler option to shrink the code.
...
I honestly do not understand your obsession to the code size. You did not publish your sketch, but are you aware that even a "blank sketch" has a size? Try it ... just compile:

Code: Select all

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
There are numerous Internet articles explaining that no source code != no flash size usage.