From 6d1d5ccb1c66446f2f25c02c06aaf559be9ee8d5 Mon Sep 17 00:00:00 2001 From: Alexey Lapshin Date: Thu, 21 Nov 2024 17:37:16 +0700 Subject: [PATCH 1/2] feat(build): add CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR choise option Allow to disable implicit inlining of constexpr functions from libstdc++. This is a known GCC issue https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93008 that may affect C++ application size depending on its structure. --- CMakeLists.txt | 8 +++++++ Kconfig | 28 +++++++++++++++++++++++++ docs/en/api-guides/performance/size.rst | 5 +++++ 3 files changed, 41 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d21d2a38ec..be10166d5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,14 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATE list(APPEND c_compile_options "-fzero-init-padding-bits=all" "-fno-malloc-dce") endif() +if(CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD_CONSTEXPR) + list(APPEND cxx_compile_options "-D_GLIBCXX20_CONSTEXPR=__attribute__((cold)) constexpr") + list(APPEND cxx_compile_options "-D_GLIBCXX23_CONSTEXPR=__attribute__((cold)) constexpr") +elseif(CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD) + list(APPEND cxx_compile_options "-D_GLIBCXX20_CONSTEXPR=__attribute__((cold))") + list(APPEND cxx_compile_options "-D_GLIBCXX23_CONSTEXPR=__attribute__((cold))") +endif() + __generate_prefix_map(prefix_map_compile_options) list(APPEND compile_options ${prefix_map_compile_options}) diff --git a/Kconfig b/Kconfig index 5e19164a5e..a7739d04af 100644 --- a/Kconfig +++ b/Kconfig @@ -685,6 +685,34 @@ mainmenu "Espressif IoT Development Framework Configuration" help Enable compiler static analyzer. This may produce false-positive results and increases compile time. + choice COMPILER_CXX_GLIBCXX_CONSTEXPR + prompt "Define _GLIBCXX_CONSTEXPR" + default COMPILER_CXX_GLIBCXX_CONSTEXPR_NO_CHANGE + depends on IDF_TOOLCHAIN_GCC && !IDF_TARGET_LINUX + help + Modify libstdc++ _GLIBCXX20_CONSTEXPR and _GLIBCXX23_CONSTEXPR definitions to provide size + optimizations. The total size optimization depends on the application's structure. + There is no robust way to determine which option would be better in a particular case. + Please try all available options to find the best size optimization. + + config COMPILER_CXX_GLIBCXX_CONSTEXPR_NO_CHANGE + bool "No change" + help + Use default _GLIBCXX20_CONSTEXPR and _GLIBCXX23_CONSTEXPR defined in libstdc++ + + config COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD_CONSTEXPR + bool "_GLIBCXX2X_CONSTEXPR=__attribute__((cold)) constexpr" + help + Define _GLIBCXX20_CONSTEXPR=__attribute__((cold)) constexpr + Define _GLIBCXX23_CONSTEXPR=__attribute__((cold)) constexpr + + config COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD + bool "_GLIBCXX2X_CONSTEXPR=__attribute__((cold))" + help + Define _GLIBCXX20_CONSTEXPR=__attribute__((cold)). + Define _GLIBCXX23_CONSTEXPR=__attribute__((cold)). + endchoice + endmenu # Compiler Options menu "Component config" diff --git a/docs/en/api-guides/performance/size.rst b/docs/en/api-guides/performance/size.rst index 45e4004bc4..7cd74d538b 100644 --- a/docs/en/api-guides/performance/size.rst +++ b/docs/en/api-guides/performance/size.rst @@ -194,6 +194,11 @@ Enabling "Nano" formatting reduces the stack usage of each function that calls ` .. _Newlib README file: https://sourceware.org/newlib/README +libstdc++ +@@@@@@@@@ + +- Enable :ref:`CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD_CONSTEXPR` or :ref:`CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD` to observe the impact on your application's binary size. + .. _minimizing_binary_mbedtls: MbedTLS Features From f53f02f3b52f5121728a819eb4317735726dc5b5 Mon Sep 17 00:00:00 2001 From: renpeiying Date: Sun, 27 Apr 2025 15:35:04 +0800 Subject: [PATCH 2/2] docs(size): update CN translation for size.rst --- docs/zh_CN/api-guides/performance/size.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/zh_CN/api-guides/performance/size.rst b/docs/zh_CN/api-guides/performance/size.rst index f7696d0792..056663b516 100644 --- a/docs/zh_CN/api-guides/performance/size.rst +++ b/docs/zh_CN/api-guides/performance/size.rst @@ -194,6 +194,11 @@ ESP-IDF 的 I/O 函数( ``printf()`` 和 ``scanf()`` 等)默认使用 Newlib .. _Newlib README 文件: https://sourceware.org/newlib/README +libstdc++ +@@@@@@@@@ + +- 启用 :ref:`CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD_CONSTEXPR` 或 :ref:`CONFIG_COMPILER_CXX_GLIBCXX_CONSTEXPR_COLD` 观察对应用程序二进制大小的影响。 + .. _minimizing_binary_mbedtls: MbedTLS 功能