Skip to content

Commit

Permalink
Merge branch 'tools/gcc_string_1byte_align' into 'master'
Browse files Browse the repository at this point in the history
tools(lto): Support to set string 1-byte align to reduce firmware size

See merge request ae_group/esp-iot-solution!833
  • Loading branch information
wujiangang committed Aug 23, 2023
2 parents 0c837c6 + 25ff3bb commit 5f9e631
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
4 changes: 4 additions & 0 deletions tools/cmake_utilities/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.5.1 - 2023-08-22

* Add string 1-byte align support

## v0.5.0 - 2023-08-02

* Add GCC LTO support
Expand Down
8 changes: 8 additions & 0 deletions tools/cmake_utilities/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ menu "CMake Utilities"
help
"Enable this option, users can enable GCC link time optimization(LTO)
feature for target components or dependencies.

config CU_GCC_STRING_1BYTE_ALIGN
bool "GCC string 1-byte align"
default n
help
"Enable this option, user can make string in designated components align
by 1-byte instead 1-word(4-byte), this can reduce unnecessary filled data
so that to reduce firmware size."
endmenu
2 changes: 1 addition & 1 deletion tools/cmake_utilities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ This component is aiming to provide some useful CMake utilities outside of ESP-I

1. [relinker](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/relinker.md)
2. [gen_compressed_ota](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/gen_compressed_ota.md)
3. [Link Time Optimization](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/gcc_lto.md)
3. [GCC Optimization](https://github.com/espressif/esp-iot-solution/blob/master/tools/cmake_utilities/docs/gcc.md)
2 changes: 1 addition & 1 deletion tools/cmake_utilities/cmake_utilities.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Include all cmake modules

include(gcc_lto)
include(gcc)
include(gen_compressed_ota)
include(gen_single_bin)
include(package_manager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ project(XXXX)
include(cmake_utilities)
#or
include(gcc_lto)
include(gcc)
```

The LTO feature is disabled by default. To use it, you should enable the option `CU_GCC_LTO_ENABLE` in menuconfig. Then specify target components or dependencies to be optimized by LTO after `include(cmake_utilities)` or `include(gcc_lto)` as follows:
The LTO feature is disabled by default. To use it, you should enable the option `CU_GCC_LTO_ENABLE` in menuconfig. Then specify target components or dependencies to be optimized by LTO after `include(cmake_utilities)` or `include(gcc)` as follows:

```cmake
include(cmake_utilities)
#or
include(gcc_lto)
include(gcc)
cu_gcc_lto_set(COMPONENTS component_a component_b
DEPENDS dependence_a dependence_b)
cu_gcc_string_1byte_align(COMPONENTS component_c component_d
DEPENDS dependence_c dependence_d)
```

Based on your requirement, set compiling optimization level in the option `COMPILER_OPTIMIZATION`.
Expand All @@ -36,6 +39,7 @@ Based on your requirement, set compiling optimization level in the option `COMPI
2. Increasing performance may increase firmware size
3. Enable LTO cause compiling time cost increases a lot
4. Enable LTO may increase task stack cost
5. Enable string 1-byte align may decrease string process speed
```

## Limitation
Expand Down Expand Up @@ -69,7 +73,7 @@ project(light)
# Add
include(cmake_utilities)
#or
include(gcc_lto)
include(gcc)
# Add
set(app_lto_components main chip esp_matter)
Expand All @@ -81,7 +85,7 @@ set(lto_depends mbedcrypto)
# Add
cu_gcc_lto_set(COMPONENTS ${app_lto_components} ${idf_lto_components}
DEPENDS ${lto_depends})
````
```

Configure `ESP32-C2` as the target platform, enable `CU_GCC_LTO_ENABLE` and `CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE`, set `COMPILER_OPTIMIZATION` to be `-Os`.
Increase the `main` task stack size to `5120` by option `ESP_MAIN_TASK_STACK_SIZE`.
Expand All @@ -91,3 +95,21 @@ Option | Firmware size | Stask cost
|:-:|:-:|:-:|
-Os | 1,113,376 | 2508
-Os + LTO | 1,020,640 | 4204

Then add `cu_gcc_string_1byte_align` after `cu_gcc_lto_set`:

```cmake
# Add
cu_gcc_lto_set(COMPONENTS ${app_lto_components} ${idf_lto_components}
DEPENDS ${lto_depends})
cu_gcc_string_1byte_align(COMPONENTS ${app_lto_components} ${idf_lto_components}
DEPENDS ${lto_depends})
```

Build the project and the firmware size is:

Option | Firmware size |
|:-:|:-:|:-:|
-Os + LTO | 1,020,640 |
-Os + LTO + string 1-byte align | 1,018,340 |
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,27 @@ else()
message(STATUS "GCC link time optimization(LTO) is not enable")
endmacro()
endif()

if(CONFIG_CU_GCC_STRING_1BYTE_ALIGN)
macro(cu_gcc_string_1byte_align)
message(STATUS "GCC string 1-byte align is enable")

set(multi_value COMPONENTS DEPENDS)
cmake_parse_arguments(STR_ALIGN "" "" "${multi_value}" ${ARGN})

message(STATUS "GCC string 1-byte align for components: ${STR_ALIGN_COMPONENTS}")
foreach(c ${STR_ALIGN_COMPONENTS})
idf_component_get_property(t ${c} COMPONENT_LIB)
target_compile_options(${t} PRIVATE "-malign-data=natural")
endforeach()

message(STATUS "GCC string 1-byte align for dependencies: ${STR_ALIGN_DEPENDS}")
foreach(d ${STR_ALIGN_DEPENDS})
target_compile_options(${d} PRIVATE "-malign-data=natural")
endforeach()
endmacro()
else()
macro(cu_gcc_string_1byte_align)
message(STATUS "GCC string 1-byte align is not enable")
endmacro()
endif()
2 changes: 1 addition & 1 deletion tools/cmake_utilities/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.5.0"
version: "0.5.1"
description: A collection of useful cmake utilities
url: https://github.com/espressif/esp-iot-solution/tree/master/tools/cmake_utilities
issues: https://github.com/espressif/esp-iot-solution/issues
Expand Down

0 comments on commit 5f9e631

Please sign in to comment.