Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ target_link_libraries(kernel zephyr_interface)

endif()

# Optionally build kernel sources without LTO
if(CONFIG_KERNEL_NO_LTO)
include(${CMAKE_CURRENT_LIST_DIR}/kernel_no_lto.cmake)
endif()

add_dependencies(kernel zephyr_generated_headers)

unset(libkernel)
22 changes: 22 additions & 0 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,28 @@ endif # BOOTARGS

endmenu

config KERNEL_NO_LTO
bool "Build selected kernel core files without LTO"
depends on LTO
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.

if KERNEL_NO_LTO
config KERNEL_LTO_ALLOWLIST
string "List of kernel source files to keep LTO"
help
List of kernel source filenames that should retain LTO even when
CONFIG_KERNEL_NO_LTO is enabled.
Example: CONFIG_KERNEL_LTO_ALLOWLIST="init.c errno.c fatal.c"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to make these files the default setting? Otherwise you could expand the help text to suggest this list as a good default as these features don't benefit from the performance gain of running in RAM.


endif # KERNEL_NO_LTO

rsource "Kconfig.device"
rsource "Kconfig.vm"
rsource "Kconfig.init"
42 changes: 42 additions & 0 deletions kernel/kernel_no_lto.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SPDX-License-Identifier: Apache-2.0

# Optional script to disable LTO for kernel files

message(STATUS "[no-LTO] Building kernel files without LTO")

# Retrieve all source files from the kernel library target
if(TARGET kernel)
get_property(KERNEL_SRCS TARGET kernel PROPERTY SOURCES)
else()
message(WARNING "[no-LTO] kernel target not found, skipping")
return()
endif()

# Split allowlist string into list
if(DEFINED CONFIG_KERNEL_LTO_ALLOWLIST)
separate_arguments(LTO_ALLOWLIST NATIVE_COMMAND "${CONFIG_KERNEL_LTO_ALLOWLIST}")
endif()

# Apply -fno-lto to all C source files, except for some initialization files
# (e.g. init.c, errno.c, fatal.c) and those that are less critical to be
# placed in RAM. These files can be excluded from -fno-lto by using
# CONFIG_KERNEL_LTO_ALLOWLIST.
foreach(src ${KERNEL_SRCS})
if(src MATCHES "\\.c$")

# Skip if filename matches any in allowlist
set(skip FALSE)
foreach(allow ${LTO_ALLOWLIST})
get_filename_component(basename ${src} NAME)
if("${basename}" STREQUAL "${allow}")
set(skip TRUE)
break()
endif()
endforeach()

if(NOT skip)
set_source_files_properties(${src} PROPERTIES COMPILE_FLAGS "-fno-lto -g")
endif()

endif()
endforeach()
1 change: 1 addition & 0 deletions soc/ite/ec/it8xxx2/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ config SOC_IT8XXX2_KERNEL_IN_RAM
bool "Place kernel handling code in RAM"
select SOC_IT8XXX2_USE_ILM
select SOC_IT8XXX2_LIBRARY_TO_RAM
select KERNEL_NO_LTO if LTO
help
Place kernel handling code in ILM. This can significantly improve performance.

Expand Down