zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
9.65k stars 5.95k forks source link

ADC will error with CONFIG_ADC combined with CONFIG_LTO and CONFIG_DEBUG_OPTIMIZATIONS #71689

Open UmbraMalison opened 1 week ago

UmbraMalison commented 1 week ago

Description ZEPHYR-v3.6.0 release notes indicate the availability of Link-Time-Optimisations, which were suggested to me as a opportunity to reduce ROM size. In release mode, (without CONFIG_DEBUG_OPTIMIZATIONS) LTO saved me about 3KB (from a flash size of 32KB, so I was very happy).

My project compiles just fine with the following options:

CONFIG_ADC=y
CONFIG_LTO=y
CONFIG_ISR_TABLES_LOCAL_DECLARATION=y

or with:

CONFIG_ADC=y
CONFIG_DEBUG_OPTIMIZATIONS=y

but ADC will error when LTO is combined with DEBUG_OPTIMIZATIONS:

CONFIG_ADC=y
CONFIG_LTO=y
CONFIG_ISR_TABLES_LOCAL_DECLARATION=y
CONFIG_DEBUG_OPTIMIZATIONS=y
west build -b nucleo_g071rb .\samples\basic\blinky\ --pristine -- -DCONFIG_DEBUG_OPTIMIZATIONS=y -DCONFIG_LTO=y -DCONFIG_ISR_TABLES_LOCAL_DECLARATION=y -DCONFIG_NEWLIB_LIBC=n -DCONFIG_ADC=y                  
-- west build: making build dir C:\Development\projects\myproject\firmware\zephyr\build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: C:/Development/projects/myproject/firmware/zephyr/samples/basic/blinky
-- CMake version: 3.28.2
-- Found Python3: C:/Users/andy/zephyr-venv/Scripts/python.exe (found suitable version "3.11.9", minimum required is "3.8") found components: Interpreter 
-- Cache files will be written to: C:/Development/projects/myproject/firmware/zephyr/.cache
-- Zephyr version: 3.6.0 (C:/Development/projects/myproject/firmware/zephyr)
-- Found west (found suitable version "1.2.0", minimum required is "0.14.0")
-- Board: nucleo_g071rb
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.16.5 (C:/Users/andy/zephyr-sdk-0.16.5-1)
-- Found toolchain: zephyr 0.16.5 (C:/Users/andy/zephyr-sdk-0.16.5-1)
-- Found Dtc: C:/msys64/mingw64/bin/dtc.exe (found suitable version "1.6.0", minimum required is "1.4.6") 
-- Found BOARD.dts: C:/Development/projects/myproject/firmware/zephyr/boards/arm/nucleo_g071rb/nucleo_g071rb.dts
-- Generated zephyr.dts: C:/Development/projects/myproject/firmware/zephyr/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: C:/Development/projects/myproject/firmware/zephyr/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: C:/Development/projects/myproject/firmware/zephyr/build/zephyr/dts.cmake
Parsing C:/Development/projects/myproject/firmware/zephyr/Kconfig
Loaded configuration 'C:/Development/projects/myproject/firmware/zephyr/boards/arm/nucleo_g071rb/nucleo_g071rb_defconfig'
Merged configuration 'C:/Development/projects/myproject/firmware/zephyr/samples/basic/blinky/prj.conf'
Merged configuration 'C:/Development/projects/myproject/firmware/zephyr/build/zephyr/misc/generated/extra_kconfig_options.conf'
Configuration saved to 'C:/Development/projects/myproject/firmware/zephyr/build/zephyr/.config'
Kconfig header saved to 'C:/Development/projects/myproject/firmware/zephyr/build/zephyr/include/generated/autoconf.h'
-- Found GnuLd: c:/users/andy/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe (found version "2.38") 
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: C:/Users/andy/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe
-- Configuring done (5.6s)
-- Generating done (0.2s)
-- Build files have been written to: C:/Development/projects/myproject/firmware/zephyr/build
-- west build: building application
[1/132] Generating include/generated/version.h
-- Zephyr version: 3.6.0 (C:/Development/projects/myproject/firmware/zephyr), build: v3.6.0
[68/132] Building C object modules/gpio_mux/CMakeFiles/..__my_modules__drivers__gpio_mux.dir/gpio_mux.c.obj
C:/Development/projects/myproject/firmware/my_modules/drivers/gpio_mux/gpio_mux.c: In function 'gpio_mux_init':
C:/Development/projects/myproject/firmware/my_modules/drivers/gpio_mux/gpio_mux.c:197:31: warning: unused variable 'data' [-Wunused-variable]
  197 |         struct gpio_mux_data *data = dev->data;
      |                               ^~~~
C:/Development/projects/myproject/firmware/my_modules/drivers/gpio_mux/gpio_mux.c: At top level:
C:/Development/projects/myproject/firmware/my_modules/drivers/gpio_mux/gpio_mux.c:194:12: warning: 'gpio_mux_init' defined but not used [-Wunused-function]
  194 | static int gpio_mux_init(const struct device *dev)
      |            ^~~~~~~~~~~~~
C:/Development/projects/myproject/firmware/my_modules/drivers/gpio_mux/gpio_mux.c:167:37: warning: 'gpio_mux_api' defined but not used [-Wunused-const-variable=]
  167 | static const struct gpio_driver_api gpio_mux_api = {
      |                                     ^~~~~~~~~~~~
[126/132] Linking C executable zephyr\zephyr_pre0.elf
FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map C:/Development/projects/myproject/firmware/zephyr/build/zephyr/zephyr_pre0.map
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\Users\andy\zephyr-sdk-0.16.5-1\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe  -gdwarf-4 -flto -fno-ipa-sra -ffunction-sections -fdata-sections zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr\zephyr_pre0.elf  zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj  -fuse-ld=bfd  -T  zephyr/linker_zephyr_pre0.cmd  -Wl,-Map=C:/Development/projects/myproject/firmware/zephyr/build/zephyr/zephyr_pre0.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/common/libarch__common.a  zephyr/arch/arch/arm/core/libarch__arm__core.a  zephyr/arch/arch/arm/core/cortex_m/libarch__arm__core__cortex_m.a  zephyr/arch/arch/arm/core/mpu/libarch__arm__core__mpu.a  zephyr/lib/libc/picolibc/liblib__libc__picolibc.a  zephyr/lib/libc/common/liblib__libc__common.a  zephyr/drivers/interrupt_controller/libdrivers__interrupt_controller.a  zephyr/drivers/adc/libdrivers__adc.a  zephyr/drivers/clock_control/libdrivers__clock_control.a  zephyr/drivers/console/libdrivers__console.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/pinctrl/libdrivers__pinctrl.a  zephyr/drivers/reset/libdrivers__reset.a  zephyr/drivers/serial/libdrivers__serial.a  zephyr/drivers/timer/libdrivers__timer.a  modules/gpio_mux/lib..__my_modules__drivers__gpio_mux.a  modules/stm32/stm32cube/lib..__modules__hal__stm32__stm32cube.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  -L"c:/users/andy/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/thumb/v6-m/nofp"  -LC:/Development/projects/myproject/firmware/zephyr/build/zephyr  -lgcc  zephyr/arch/common/libisr_tables.a  -mcpu=cortex-m0plus  -mthumb  -mabi=aapcs  -mfp16-format=ieee  -mtp=soft  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -nostdlib  -static  -Wl,-X  -Wl,-N  -Wl,--orphan-handling=warn  -Wl,-no-pie  -DPICOLIBC_LONG_LONG_PRINTF_SCANF  --specs=picolibc.specs  -lc  -lgcc && C:\WINDOWS\system32\cmd.exe /C "cd /D C:\Development\projects\myproject\firmware\zephyr\build\zephyr && "C:\Program Files\CMake\bin\cmake.exe" -E true""
C:/Development/projects/myproject/firmware/zephyr/kernel/sched.c: In function 'z_timer_expiration_handler':
C:/Development/projects/myproject/firmware/zephyr/kernel/sched.c:883:20: error: inlining failed in call to 'always_inline' 'z_unpend_thread_no_timeout': function not considered for inlining      
  883 | ALWAYS_INLINE void z_unpend_thread_no_timeout(struct k_thread *thread)
      |                    ^
C:/Development/projects/myproject/firmware/zephyr/kernel/timer.c:103:9: note: called from here
  103 |         z_unpend_thread_no_timeout(thread);
      |         ^
lto-wrapper.exe: fatal error: C:\Users\andy\zephyr-sdk-0.16.5-1\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe returned 1 exit status
compilation terminated.
c:/users/andy/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: 'C:\Program Files\CMake\bin\cmake.EXE' --build 'C:\Development\projects\myproject\firmware\zephyr\build'

To Reproduce Steps to reproduce the behavior:

west build -b nucleo_g071rb .\samples\basic\blinky\ --pristine -- -DCONFIG_ADC=y -DCONFIG_DEBUG_OPTIMIZATIONS=y -DCONFIG_LTO=y -DCONFIG_ISR_TABLES_LOCAL_DECLARATION=y

Expected behavior Expected the build to complete with same or less flash utilisation for debugging/development builds.

Impact without LTO, on this uC (32KB) it's much closer to overflowing the flash available. For debugging, I need more space for debug symbols. Without LTO: 89%, with LTO it's 83%. With debugging optimisations it's 98%, if LTO can help achieve the same improvements then I might see 92% with debug symbols - which would be great.

Logs and console output None

Environment (please complete the following information):

github-actions[bot] commented 1 week ago

Hi @UmbraMalison! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

dkalowsk commented 1 week ago

I took a brief look at this, I don't think this really has anything to do with the ADC and more with the ALWAYS_INLINE flag being used. Specifically I believe in LTO builds that flag probably would do better to evaluate out to just the inline flag (which is more a compiler hint/suggestion) and not the attribute(always_inline) (which is more a demand on the compiler) as the LTO process really doesn't like competition with determining who/what to inline.