From d6041d62b338c1ad4dee7f7a92bb9c2f9f7aa9bf Mon Sep 17 00:00:00 2001 From: Jonathon Penix Date: Wed, 20 Mar 2024 10:29:51 -0700 Subject: [PATCH] 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 --- CMakeLists.txt | 3 +++ Kconfig.zephyr | 6 ++++++ cmake/compiler/compiler_flags_template.cmake | 2 ++ cmake/compiler/gcc/compiler_flags.cmake | 1 + lib/libc/minimal/Kconfig | 2 +- modules/trusted-firmware-m/Kconfig.tfm | 2 +- 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4dd40cf66f..11ecf1e3961 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_SPEED_FLAG TARGET compiler PROPERTY optimization_speed) 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. # 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}) elseif(CONFIG_SIZE_OPTIMIZATIONS) set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig +elseif(CONFIG_SIZE_OPTIMIZATIONS_AGGRESSIVE) + set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG}) else() message(FATAL_ERROR "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr") diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 01b5f4497e0..5e6694b1c21 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -490,6 +490,12 @@ config SIZE_OPTIMIZATIONS Compiler optimizations will be set to -Os independently of other 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 bool "Optimize for speed" help diff --git a/cmake/compiler/compiler_flags_template.cmake b/cmake/compiler/compiler_flags_template.cmake index a18ef39d916..c72e9b70f5d 100644 --- a/cmake/compiler/compiler_flags_template.cmake +++ b/cmake/compiler/compiler_flags_template.cmake @@ -13,6 +13,8 @@ set_compiler_property(PROPERTY optimization_speed) set_compiler_property(PROPERTY optimization_size) +set_compiler_property(PROPERTY optimization_size_aggressive) + ####################################################### # This section covers flags related to warning levels # ####################################################### diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake index be08cc18bad..e982f75aa78 100644 --- a/cmake/compiler/gcc/compiler_flags.cmake +++ b/cmake/compiler/gcc/compiler_flags.cmake @@ -20,6 +20,7 @@ else() endif() set_compiler_property(PROPERTY optimization_speed -O2) 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") set_compiler_property(PROPERTY optimization_lto -flto) diff --git a/lib/libc/minimal/Kconfig b/lib/libc/minimal/Kconfig index a55bf7a87b2..8b7b07bf5a8 100644 --- a/lib/libc/minimal/Kconfig +++ b/lib/libc/minimal/Kconfig @@ -63,7 +63,7 @@ config MINIMAL_LIBC_LL_PRINTF config MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE bool "Use size optimized string functions" - default y if SIZE_OPTIMIZATIONS + default y if SIZE_OPTIMIZATIONS || SIZE_OPTIMIZATIONS_AGGRESSIVE help Enable smaller but potentially slower implementations of memcpy and memset. On the Cortex-M0+ this reduces the total code size by 120 bytes. diff --git a/modules/trusted-firmware-m/Kconfig.tfm b/modules/trusted-firmware-m/Kconfig.tfm index 8834256ccbb..51fce0589b7 100644 --- a/modules/trusted-firmware-m/Kconfig.tfm +++ b/modules/trusted-firmware-m/Kconfig.tfm @@ -93,7 +93,7 @@ endchoice choice TFM_CMAKE_BUILD_TYPE prompt "The build type for TFM" 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_RELWITHDEBINFO