Zephry LTO optimization

Hello!

Code size is quite crucial in our project, naturally CONFIG_SIZE_OPTIMIZATIONS is enabled. In other projects I've experience with LTO (link time optimization), this feature could cut off a few more percents from final binary size. Alas I haven't seen any easily reachable option to activate LTO in Zephyr.

My best guess was the following in CMakeLists.txt:

zephyr_ld_options("-flto")

but the resulting binary size remained the same. I've digged into the build directory and found this in build.ninja:

...
LINK_FLAGS = -gdwarf-4
LINK_LIBRARIES = ... -flto
...

This tells me the build system added -flto to LINK_LIBRARIES instead of LINK_FLAGS. How can I make Zephyr build system use my custom linker flag? Possibly without hacking, the standard Zephyr way is preferred...

Parents Reply Children
  • I used to have success with LTO in my other projects during the recent years. The required technique usually was to specify -flto both during compile and linking, and at the cost of a much longer linking the resulting binary size was 3-5-7% less than without LTO. In some circumstances this can mean a lot ;)

    I tried the statement found in the PR with more NCS-2.2.0 and NCS-2.4.1 applications, and surprisingly the final binary was usually greater than without LTO!

    Is there any way forcing NCS to use -lflto not only for the application code, but for the rest (Zephyr kernel) too? I'd give it a try...

    Thanks,

Related