kernel: Add Kconfig option to disable LTO for kernel sources

Some SoCs require kernel code to be placed in RAM, which makes
link-time optimization (LTO) unsuitable for these files.
Disabling LTO allows the affected code to be linked as separate
objects and placed in specific memory regions.

Running kernel code from RAM can improve execution performance,
especially for timing-critical routines or context switch paths.

Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
This commit is contained in:
Tim Lin
2025-11-11 08:00:21 +08:00
committed by Henrik Brix Andersen
parent 4d0852b005
commit 2edece9d58
3 changed files with 21 additions and 1 deletions

View File

@@ -315,7 +315,9 @@ if(CONFIG_LTO)
zephyr_compile_options($<TARGET_PROPERTY:compiler,optimization_lto_st>)
add_link_options($<TARGET_PROPERTY:linker,lto_arguments_st>)
else()
zephyr_compile_options($<TARGET_PROPERTY:compiler,optimization_lto>)
set(genex_tgt_lto "$<TARGET_PROPERTY:LTO>")
set(genex_lto "$<TARGET_PROPERTY:compiler,optimization_lto>")
zephyr_compile_options("$<$<OR:$<STREQUAL:${genex_tgt_lto},>,${genex_tgt_lto}>:${genex_lto}>")
add_link_options($<TARGET_PROPERTY:linker,lto_arguments>)
endif()
endif()

View File

@@ -195,6 +195,11 @@ target_link_libraries(kernel zephyr_interface)
endif()
# Optionally build kernel sources without LTO
if(CONFIG_KERNEL_NO_LTO)
set_target_properties(kernel PROPERTIES LTO 0)
endif()
add_dependencies(kernel zephyr_generated_headers)
unset(libkernel)

View File

@@ -1165,6 +1165,19 @@ endif # BOOTARGS
endmenu
config KERNEL_NO_LTO
bool
depends on LTO
depends on XIP
help
Some SoCs require kernel code to be placed in RAM, which makes link-time
optimization (LTO) unsuitable for these files (-fno-lto). Disabling LTO
allows the affected code to be linked as separate objects and placed in
specific memory regions.
Running kernel code from RAM can improve execution performance, especially
for timing-critical routines or context switch paths.
rsource "Kconfig.device"
rsource "Kconfig.vm"
rsource "Kconfig.init"