cmake: kconfig: Add new abstraction for -Oz optimization level

Both Clang [1] and (recently) GCC [2] support this flag to enable
additional codesize optimizations beyond -Os, possibly at the expense of
performance.

This tradeoff is worthwhile for some (and, Clang's -Oz seems to be
closer to GCC's -Os currently), so add a new abstraction for this flag
so users can select it as appropriate.

[1] https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-O0
[2] https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-Oz

Signed-off-by: Jonathon Penix <jpenix@quicinc.com>
This commit is contained in:
Jonathon Penix 2024-03-20 10:29:51 -07:00 committed by Anas Nashif
commit d6041d62b3
6 changed files with 14 additions and 2 deletions

View file

@ -192,6 +192,7 @@ get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_opti
get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug) get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed) get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size) get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
get_property(OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG TARGET compiler PROPERTY optimization_size_aggressive)
# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use. # From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set. # Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
@ -203,6 +204,8 @@ elseif(CONFIG_SPEED_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG}) set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
elseif(CONFIG_SIZE_OPTIMIZATIONS) elseif(CONFIG_SIZE_OPTIMIZATIONS)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
elseif(CONFIG_SIZE_OPTIMIZATIONS_AGGRESSIVE)
set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG})
else() else()
message(FATAL_ERROR message(FATAL_ERROR
"Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr") "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")

View file

@ -490,6 +490,12 @@ config SIZE_OPTIMIZATIONS
Compiler optimizations will be set to -Os independently of other Compiler optimizations will be set to -Os independently of other
options. options.
config SIZE_OPTIMIZATIONS_AGGRESSIVE
bool "Aggressively optimize for size"
help
Compiler optimizations wil be set to -Oz independently of other
options.
config SPEED_OPTIMIZATIONS config SPEED_OPTIMIZATIONS
bool "Optimize for speed" bool "Optimize for speed"
help help

View file

@ -13,6 +13,8 @@ set_compiler_property(PROPERTY optimization_speed)
set_compiler_property(PROPERTY optimization_size) set_compiler_property(PROPERTY optimization_size)
set_compiler_property(PROPERTY optimization_size_aggressive)
####################################################### #######################################################
# This section covers flags related to warning levels # # This section covers flags related to warning levels #
####################################################### #######################################################

View file

@ -20,6 +20,7 @@ else()
endif() endif()
set_compiler_property(PROPERTY optimization_speed -O2) set_compiler_property(PROPERTY optimization_speed -O2)
set_compiler_property(PROPERTY optimization_size -Os) set_compiler_property(PROPERTY optimization_size -Os)
set_compiler_property(PROPERTY optimization_size_aggressive -Oz)
if(CMAKE_C_COMPILER_VERSION GREATER_EQUAL "4.5.0") if(CMAKE_C_COMPILER_VERSION GREATER_EQUAL "4.5.0")
set_compiler_property(PROPERTY optimization_lto -flto) set_compiler_property(PROPERTY optimization_lto -flto)

View file

@ -63,7 +63,7 @@ config MINIMAL_LIBC_LL_PRINTF
config MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE config MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE
bool "Use size optimized string functions" bool "Use size optimized string functions"
default y if SIZE_OPTIMIZATIONS default y if SIZE_OPTIMIZATIONS || SIZE_OPTIMIZATIONS_AGGRESSIVE
help help
Enable smaller but potentially slower implementations of memcpy and Enable smaller but potentially slower implementations of memcpy and
memset. On the Cortex-M0+ this reduces the total code size by 120 bytes. memset. On the Cortex-M0+ this reduces the total code size by 120 bytes.

View file

@ -93,7 +93,7 @@ endchoice
choice TFM_CMAKE_BUILD_TYPE choice TFM_CMAKE_BUILD_TYPE
prompt "The build type for TFM" prompt "The build type for TFM"
default TFM_CMAKE_BUILD_TYPE_RELEASE if SPEED_OPTIMIZATIONS && BUILD_OUTPUT_STRIPPED default TFM_CMAKE_BUILD_TYPE_RELEASE if SPEED_OPTIMIZATIONS && BUILD_OUTPUT_STRIPPED
default TFM_CMAKE_BUILD_TYPE_MINSIZEREL if SIZE_OPTIMIZATIONS default TFM_CMAKE_BUILD_TYPE_MINSIZEREL if SIZE_OPTIMIZATIONS || SIZE_OPTIMIZATIONS_AGGRESSIVE
default TFM_CMAKE_BUILD_TYPE_DEBUG if DEBUG_OPTIMIZATIONS default TFM_CMAKE_BUILD_TYPE_DEBUG if DEBUG_OPTIMIZATIONS
default TFM_CMAKE_BUILD_TYPE_RELWITHDEBINFO default TFM_CMAKE_BUILD_TYPE_RELWITHDEBINFO