diff --git a/CMakeLists.txt b/CMakeLists.txt index f580e8de85..4be17e206d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,10 @@ else() list(APPEND link_options "-fno-rtti") # used to invoke correct multilib variant (no-rtti) during linking endif() +if(CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS) + list(APPEND compile_options "-msave-restore") +endif() + if(CONFIG_COMPILER_DISABLE_GCC8_WARNINGS) list(APPEND compile_options "-Wno-parentheses" "-Wno-sizeof-pointer-memaccess" diff --git a/Kconfig b/Kconfig index 65d03788c9..928d274106 100644 --- a/Kconfig +++ b/Kconfig @@ -394,6 +394,18 @@ mainmenu "Espressif IoT Development Framework Configuration" For C++, this warns about the deprecated conversion from string literals to ``char *``. + config COMPILER_SAVE_RESTORE_LIBCALLS + bool "Enable -msave-restore flag to reduce code size" + depends on IDF_TARGET_ARCH_RISCV + help + Adds -msave-restore to C/C++ compilation flags. + + When this flag is enabled, compiler will call library functions to + save/restore registers in function prologues/epilogues. This results + in lower overall code size, at the expense of slightly reduced performance. + + This option can be enabled for RISC-V targets only. + config COMPILER_DISABLE_GCC8_WARNINGS bool "Disable new warnings introduced in GCC 6 - 8" default "n" diff --git a/components/newlib/system_libs.lf b/components/newlib/system_libs.lf index f3b35f89ca..7480a12697 100644 --- a/components/newlib/system_libs.lf +++ b/components/newlib/system_libs.lf @@ -4,6 +4,8 @@ entries: lib2funcs (noflash_text) if IDF_TARGET_ESP32 = n: _divsf3 (noflash) + if IDF_TARGET_ARCH_RISCV: + save-restore (noflash) [mapping:gcov] archive: libgcov.a diff --git a/docs/en/api-guides/performance/size.rst b/docs/en/api-guides/performance/size.rst index cfe293edcc..81dfd002b7 100644 --- a/docs/en/api-guides/performance/size.rst +++ b/docs/en/api-guides/performance/size.rst @@ -298,6 +298,7 @@ The following configuration options will reduce the final binary size of almost - Don't enable :ref:`CONFIG_COMPILER_CXX_EXCEPTIONS`, :ref:`CONFIG_COMPILER_CXX_RTTI`, or set the :ref:`CONFIG_COMPILER_STACK_CHECK_MODE` to Overall. All of these options are already disabled by default, but they have a large impact on binary size. - Disabling :ref:`CONFIG_ESP_ERR_TO_NAME_LOOKUP` will remove the lookup table to translate user-friendly names for error values (see :doc:`/api-guides/error-handling`) in error logs, etc. This saves some binary size, but error values will be printed as integers only. - Setting :ref:`CONFIG_ESP_SYSTEM_PANIC` to "Silent reboot" will save a small amount of binary size, however this is *only* recommended if no one will use UART output to debug the device. + :CONFIG_IDF_TARGET_ARCH_RISCV: - Set :ref:`CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS` to reduce binary size by replacing inlined prologues/epilogues with library calls. .. note::