From ee2f8b1a6236fcdcd57f8410d98119d96de40a3f Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Thu, 6 May 2021 14:54:30 +0800 Subject: [PATCH] build system: always build with -fno-jump-tables & -fno-tree-switch-conversion Jump tables placed in flash would cause issue with code that needed to be ran from IRAM. These optimizations are now always disabled. --- CMakeLists.txt | 5 +++++ components/bt/CMakeLists.txt | 6 ++++++ components/esp_hw_support/CMakeLists.txt | 5 ----- components/esp_hw_support/component.mk | 2 -- components/esp_hw_support/port/esp32/CMakeLists.txt | 5 ----- components/esp_hw_support/port/esp32c3/CMakeLists.txt | 5 ----- components/esp_hw_support/port/esp32s2/CMakeLists.txt | 5 ----- components/esp_hw_support/port/esp32s3/CMakeLists.txt | 5 ----- docs/en/api-guides/memory-types.rst | 8 ++------ docs/en/api-guides/performance/speed.rst | 2 ++ make/project.mk | 5 +++++ 11 files changed, 20 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0adb9bb17..1184e88cc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,11 @@ endif() list(APPEND link_options "-fno-lto") +# Placing jump tables in flash would cause issues with code that required +# to be placed in IRAM +list(APPEND compile_options "-fno-jump-tables") +list(APPEND compile_options "-fno-tree-switch-conversion") + idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND) idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND) idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 0f8d13df63..b9cd74e67f 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -611,3 +611,9 @@ if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE AND CONFIG_BT_NIMBLE_ENABLED) # some variables in NimBLE are only used by asserts target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-but-set-variable -Wno-unused-variable) endif() + +if(NOT CMAKE_BUILD_EARLY_EXPANSION) + set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/host/bluedroid/bta/hf_ag/bta_ag_cmd.c" + "${CMAKE_CURRENT_LIST_DIR}/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c" + PROPERTIES COMPILE_FLAGS "-fjump-tables -ftree-switch-conversion") +endif() diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index ad4d749438..2fcb16e8bb 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -18,11 +18,6 @@ if(NOT BOOTLOADER_BUILD) "sleep_modes.c" "regi2c_ctrl.c") list(APPEND priv_requires esp_ipc) - - if(NOT CMAKE_BUILD_EARLY_EXPANSION) - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/sleep_modes.c" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") - endif() endif() idf_component_register(SRCS ${srcs} diff --git a/components/esp_hw_support/component.mk b/components/esp_hw_support/component.mk index 54ddde0266..5a670db3bb 100644 --- a/components/esp_hw_support/component.mk +++ b/components/esp_hw_support/component.mk @@ -2,8 +2,6 @@ COMPONENT_SRCDIRS := . port/$(IDF_TARGET) COMPONENT_ADD_INCLUDEDIRS := . include port/$(IDF_TARGET)/ include/soc port/$(IDF_TARGET)/private_include COMPONENT_ADD_LDFRAGMENTS := linker.lf -port/$(IDF_TARGET)/rtc_clk.o: CFLAGS += -fno-jump-tables -fno-tree-switch-conversion - ifdef IS_BOOTLOADER_BUILD COMPONENT_OBJEXCLUDE += clk_ctrl_os.o mac_addr.o sp_async_memcpy.o intr_alloc.o sleep_modes.o regi2c_ctrl.o endif diff --git a/components/esp_hw_support/port/esp32/CMakeLists.txt b/components/esp_hw_support/port/esp32/CMakeLists.txt index d261f73455..2bbd509c18 100644 --- a/components/esp_hw_support/port/esp32/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32/CMakeLists.txt @@ -13,8 +13,3 @@ set(srcs add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") - -if(NOT CMAKE_BUILD_EARLY_EXPANSION) - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/rtc_clk.c" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") -endif() diff --git a/components/esp_hw_support/port/esp32c3/CMakeLists.txt b/components/esp_hw_support/port/esp32c3/CMakeLists.txt index 99b1f7929b..3738e9b305 100644 --- a/components/esp_hw_support/port/esp32c3/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32c3/CMakeLists.txt @@ -17,8 +17,3 @@ add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") target_include_directories(${COMPONENT_LIB} PUBLIC . private_include) target_include_directories(${COMPONENT_LIB} PRIVATE ../hal) - -if(NOT CMAKE_BUILD_EARLY_EXPANSION) - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/rtc_clk.c" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") -endif() diff --git a/components/esp_hw_support/port/esp32s2/CMakeLists.txt b/components/esp_hw_support/port/esp32s2/CMakeLists.txt index 3dbb8190e0..bd63e46898 100644 --- a/components/esp_hw_support/port/esp32s2/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32s2/CMakeLists.txt @@ -19,8 +19,3 @@ endif() add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") - -if(NOT CMAKE_BUILD_EARLY_EXPANSION) - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/rtc_clk.c" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") -endif() diff --git a/components/esp_hw_support/port/esp32s3/CMakeLists.txt b/components/esp_hw_support/port/esp32s3/CMakeLists.txt index 5f1a9d2f9b..740c693562 100644 --- a/components/esp_hw_support/port/esp32s3/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32s3/CMakeLists.txt @@ -18,8 +18,3 @@ endif() add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") - -if(NOT CMAKE_BUILD_EARLY_EXPANSION) - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/rtc_clk.c" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") -endif() diff --git a/docs/en/api-guides/memory-types.rst b/docs/en/api-guides/memory-types.rst index 32e2298108..002ea365aa 100644 --- a/docs/en/api-guides/memory-types.rst +++ b/docs/en/api-guides/memory-types.rst @@ -93,15 +93,11 @@ There are some possible issues with placement in IRAM, that may cause problems w Note that knowing which data should be marked with ``DRAM_ATTR`` can be hard, the compiler will sometimes recognize that a variable or expression is constant (even if it is not marked ``const``) and optimize it into flash, unless it is marked with ``DRAM_ATTR``. -* GCC optimizations that automatically generate jump tables or switch/case lookup tables place these tables in flash. There are two possible ways to resolve this issue: - - Use a :doc:`linker script fragment ` to mark the entire source file as ``noflash`` - - Pass specific flags to the compiler to disable these optimizations in the relevant source files. For CMake, place the following in the component CMakeLists.txt file: +* GCC optimizations that automatically generate jump tables or switch/case lookup tables place these tables in flash. IDF by default builds all files with `-fno-jump-tables -fno-tree-switch-conversion` flags to avoid this. - .. code-block:: cmake + Jump table optimizations can be re-enabled for individual source files that don't need to be placed in IRAM. For instructions on how to add the -fjump-tables -ftree-switch-conversion options when compiling individual source files, see :ref:`component_build_control` - set_source_files_properties("${CMAKE_CURRENT_LIST_DIR}/relative/path/to/file" PROPERTIES - COMPILE_FLAGS "-fno-jump-tables -fno-tree-switch-conversion") .. _irom: diff --git a/docs/en/api-guides/performance/speed.rst b/docs/en/api-guides/performance/speed.rst index a5d3fe4a30..87693a9f4e 100644 --- a/docs/en/api-guides/performance/speed.rst +++ b/docs/en/api-guides/performance/speed.rst @@ -116,6 +116,8 @@ The following changes will increase the speed of a chosen part of the firmware a IRAM is a limited resource, and using more IRAM may reduce available DRAM, so a strategic approach is needed when moving code to IRAM. See :ref:`iram` for more information. + - Jump table optimizations can be re-enabled for individual source files that don't need to be placed in IRAM. For hot paths in large switch cases this will improve performance. For instructions on how to add the -fjump-tables -ftree-switch-conversion options when compiling individual source files, see :ref:`component_build_control` + Improving Startup Time ---------------------- diff --git a/make/project.mk b/make/project.mk index 29e7cfe3f5..0aae810805 100644 --- a/make/project.mk +++ b/make/project.mk @@ -433,6 +433,11 @@ ifdef CONFIG_COMPILER_STACK_CHECK_MODE_ALL COMMON_FLAGS += -fstack-protector-all endif +# Placing jump tables in flash would cause issues with code that required +# to be placed in IRAM +COMMON_FLAGS += -fno-jump-tables +COMMON_FLAGS += -fno-tree-switch-conversion + # Optimization flags are set based on menuconfig choice ifdef CONFIG_COMPILER_OPTIMIZATION_SIZE OPTIMIZATION_FLAGS = -Os -freorder-blocks