Files
esp-idf/tools/cmake/toolchain-esp32c61.cmake
Alexey Lapshin ecc37c12d7 feat(build): enable -mtune=esp-base option for RISC-V targets
The `-mtune=esp-base` option is identical to the default tuning profile,
except that `slow_unaligned_access` is set to false.

This reduces the instruction count for built-in `memcpy` and improves
performance, since our chips can handle misaligned access with minimal
penalty (without triggering exceptions).

Example:

  void load(uint32_t *r, char* x) {
    memcpy(r, x, sizeof(uint32_t));
  }
  void store(char* x, uint32_t v) {
    memcpy(x, &v, sizeof(uint32_t));
  }

Previously generated code:

  load:
        lbu     a5,2(a1)
        lbu     a3,0(a1)
        lbu     a4,1(a1)
        sb      a5,2(a0)
        sb      a3,0(a0)
        sb      a4,1(a0)
        lbu     a5,3(a1)
        sb      a5,3(a0)
        ret
  store:
        srli    a3,a1,8
        srli    a4,a1,16
        srli    a5,a1,24
        addi    sp,sp,-16
        sb      a1,0(a0)
        sb      a3,1(a0)
        sb      a4,2(a0)
        sb      a5,3(a0)
        addi    sp,sp,16
        jr      ra

With `-mtune=esp-base`:

  load:
        lw      a5,0(a1)
        sw      a5,0(a0)
        ret
  store:
        sw      a1,0(a0)
        ret

Inlining behavior
=================

Without `-mtune=esp-base`:
  - `memcpy()` is inlined only when the compile-time size is ≤ 12 bytes.
    - Maximum cost: ~25 instructions

With `-mtune=esp-base`:
  - `memcpy()` is inlined for all compile-time constant sizes.
    - Maximum cost: ~14 instructions

As a result, some applications may see reduced code size, while others
may increase slightly. However, performance always improves because
extra `memcpy` calls are eliminated.

Performance results
===================

esp32p4 (Ethernet iperf):
  - No noticeable difference

esp32c61 (Wi-Fi iperf):
  - ~2 Mb/s increase for TCP and UDP TX (may be within measurement error)

NOTE
====

Applies only to RISC-V chips that do not have the hardware issue marked
by the SOC_CPU_MISALIGNED_ACCESS_ON_PMP_MISMATCH_ISSUE macro.
2025-08-27 12:46:41 +07:00

24 lines
1.2 KiB
CMake

include($ENV{IDF_PATH}/tools/cmake/utilities.cmake)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER riscv32-esp-elf-gcc)
set(CMAKE_CXX_COMPILER riscv32-esp-elf-g++)
set(CMAKE_ASM_COMPILER riscv32-esp-elf-gcc)
set(_CMAKE_TOOLCHAIN_PREFIX riscv32-esp-elf-)
set(_CMAKE_TOOLCHAIN_COMMON_FLAGS "-march=rv32imac_zicsr_zifencei_zaamo_zalrsc")
remove_duplicated_flags("${_CMAKE_TOOLCHAIN_COMMON_FLAGS} -mtune=esp-base ${CMAKE_C_FLAGS}" UNIQ_CMAKE_C_FLAGS)
set(CMAKE_C_FLAGS "${UNIQ_CMAKE_C_FLAGS}" CACHE STRING "C Compiler Base Flags" FORCE)
remove_duplicated_flags("${_CMAKE_TOOLCHAIN_COMMON_FLAGS} -mtune=esp-base ${CMAKE_CXX_FLAGS}" UNIQ_CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${UNIQ_CMAKE_CXX_FLAGS}" CACHE STRING "C++ Compiler Base Flags" FORCE)
remove_duplicated_flags("${_CMAKE_TOOLCHAIN_COMMON_FLAGS} ${CMAKE_ASM_FLAGS}" UNIQ_CMAKE_ASM_FLAGS)
set(CMAKE_ASM_FLAGS "${UNIQ_CMAKE_ASM_FLAGS}" CACHE STRING "Asm Compiler Base Flags" FORCE)
remove_duplicated_flags("-nostartfiles ${_CMAKE_TOOLCHAIN_COMMON_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}"
UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS)
set(CMAKE_EXE_LINKER_FLAGS "${UNIQ_CMAKE_SAFE_EXE_LINKER_FLAGS}" CACHE STRING "Linker Base Flags" FORCE)