Skip to content

Commit 74fadf0

Browse files
committed
Tighten the generated ESP-IDF component's sources and warnings
The generator vendored kernel util sources from KERNELS_UTIL_ALL_DEPS_SRCS, which lacks stack_util.cpp and grid_sampler_2d_util.cpp, so a profile with aten::stack.out failed to link with an undefined stack_out_impl. Both files are added to the list, which also fixes CMake's selected_kernels_util_all_deps library built from it, and the generator now globs the util directory so the next new util file cannot go missing. flatcc was vendored and compiled, but nothing in the component includes it, so it is no longer shipped. Only internal headers (tensor_parser.h and the generated schema headers) include flatbuffers, so it moves to priv_include, out of consumers' include paths where it could collide with another component's copy; FLATBUFFERS_MAX_ALIGNMENT becomes private with it. The blanket per-source -Wno-error hid every warning class. With the log format sites fixed in the previous commit, the vendored sources build clean under ESP-IDF's -Werror for ESP32-S3 and ESP32-C6, so only deprecation warnings, from ExecuTorch's own deprecated aliases, stay disabled. ET_OPTIMIZE_SIZE now defaults to y, since -Os on the runtime saves about 72 KB of code in an -O2 application. It costs speed (the ESP32-S3 smoke model went from 7.4 to 10.9 ms per frame), which the Kconfig help now says. Newlib's nano printf (CONFIG_LIBC_NEWLIB_NANO_FORMAT) has no 64-bit integer formats, and ExecuTorch's error messages print about 80 of them, so with it a logged failure misprints or crashes the app. The component now compiles ExecuTorch logging out under nano formatting and prints a CMake warning saying so; CI checks the warning. Picolibc, ESP-IDF's default, and full newlib format them correctly on Xtensa and RISC-V. Authored with Claude Code.
1 parent e8c8420 commit 74fadf0

5 files changed

Lines changed: 72 additions & 49 deletions

File tree

‎.ci/scripts/test_espressif_idf_component.sh‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ bash examples/espressif/build_idf_component.sh --profiles "${profiles}" \
4545
--out "${component_dir}" 2>&1 | tee "${build_dir}/generate.log"
4646

4747
project=examples/espressif/idf_component_smoke
48+
49+
# With newlib's nano printf, the component compiles ExecuTorch logging out and
50+
# says so.
51+
printf 'CONFIG_LIBC_NEWLIB=y\nCONFIG_LIBC_NEWLIB_NANO_FORMAT=y\n' > "${build_dir}/nano.defaults"
52+
idf.py -C "${project}" -B "${build_dir}/firmware-nano" -DIDF_TARGET=esp32s3 \
53+
-DSDKCONFIG="${build_dir}/sdkconfig-nano" \
54+
"-DSDKCONFIG_DEFAULTS=sdkconfig.defaults;${build_dir}/nano.defaults" \
55+
-DET_COMPONENT_DIR="${component_dir}" -DET_SMOKE_PTE="${build_dir}/smoke.pte" \
56+
reconfigure > "${build_dir}/firmware-nano.log" 2>&1
57+
# CMake wraps long messages, so match with whitespace runs collapsed.
58+
if ! tr -s ' \n' ' ' < "${build_dir}/firmware-nano.log" \
59+
| grep -qF "ExecuTorch logging is DISABLED"; then
60+
echo "FAIL: no nano printf warning; see ${build_dir}/firmware-nano.log" >&2
61+
exit 1
62+
fi
63+
4864
for profile in ${profiles//,/ }; do
4965
profile_upper=$(echo "${profile}" | tr '[:lower:]-' '[:upper:]_')
5066
firmware_dir="${build_dir}/firmware-${profile}"

‎examples/espressif/build_idf_component.sh‎

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
#
2626
# Host requirements: python3 with ExecuTorch + torchgen importable (for
2727
# kernel codegen), cmake (to build flatc from third-party/flatbuffers
28-
# when no flatc is available), and git submodules third-party/flatcc +
28+
# when no flatc is available), and the git submodule
2929
# third-party/flatbuffers initialized. Consumers of the output need
3030
# only ESP-IDF. Set PYTHON=... / FLATC=... to override discovery.
3131
#
3232
# NOTE: This script is coupled to the ExecuTorch source tree layout, but
33-
# its source file lists come from the same build_variables.bzl the
34-
# CMake build uses, so upstream file moves are picked up automatically.
33+
# its runtime source lists come from the same build_variables.bzl the
34+
# CMake build uses and kernel util sources are globbed, so upstream file
35+
# moves are picked up automatically.
3536
# Consumer builds for Xtensa + RISC-V targets are the safety net.
3637

3738
set -euo pipefail
@@ -106,12 +107,10 @@ if [[ ! "$ET_VERSION" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]];
106107
fi
107108
ET_COMPONENT_VERSION="${ET_VERSION}~${PACKAGING_REV}"
108109

109-
for dep in flatbuffers flatcc; do
110-
if [ ! -f "$ET_ROOT/third-party/$dep/LICENSE" ]; then
111-
echo "ERROR: third-party/$dep is empty. Run: git submodule update --init third-party/$dep" >&2
112-
exit 1
113-
fi
114-
done
110+
if [ ! -f "$ET_ROOT/third-party/flatbuffers/LICENSE" ]; then
111+
echo "ERROR: third-party/flatbuffers is empty. Run: git submodule update --init third-party/flatbuffers" >&2
112+
exit 1
113+
fi
115114

116115
# Refuse to clobber a directory that is not a previous run's output.
117116
if [ -d "$OUT_DIR" ] && [ -n "$(ls -A "$OUT_DIR" 2>/dev/null)" ] \
@@ -473,26 +472,29 @@ echo "[2/6] Kernel registration generated for: ${PROFILES[*]}"
473472

474473
# ─────────────────────────────────────────────────────────
475474
# 3. Vendor runtime + extension sources (build_variables.bzl
476-
# lists) and the union of profile kernel sources.
475+
# lists), kernel util sources and the union of profile kernel
476+
# sources.
477477
# Output mirrors upstream paths under src/executorch/.
478478
# ─────────────────────────────────────────────────────────
479479
: > "$SCRATCH/core_srcs.txt"
480480

481-
copy_bzl_list() { # $1=bzl variable name; appends to core_srcs.txt
481+
copy_srcs() { # ET_ROOT-relative paths on stdin; appends to core_srcs.txt
482482
local f
483-
bzl_list "$1" | while IFS= read -r f; do
483+
while IFS= read -r f; do
484484
if [ ! -f "$ET_ROOT/$f" ]; then
485-
echo "ERROR: build_variables.bzl lists missing file: $f" >&2; exit 1
485+
echo "ERROR: missing source file: $f" >&2; exit 1
486486
fi
487487
mkdir -p "$OUT_DIR/src/executorch/$(dirname "$f")"
488488
cp "$ET_ROOT/$f" "$OUT_DIR/src/executorch/$f"
489489
echo "src/executorch/$f" >> "$SCRATCH/core_srcs.txt"
490490
done
491491
}
492492

493-
copy_bzl_list EXECUTORCH_CORE_SRCS
494-
copy_bzl_list EXECUTORCH_SRCS # prim ops
495-
copy_bzl_list KERNELS_UTIL_ALL_DEPS_SRCS
493+
bzl_list EXECUTORCH_CORE_SRCS | copy_srcs
494+
bzl_list EXECUTORCH_SRCS | copy_srcs # prim ops
495+
# Globbed, so a util source missing from KERNELS_UTIL_ALL_DEPS_SRCS cannot
496+
# break the link.
497+
( cd "$ET_ROOT" && printf '%s\n' kernels/portable/cpu/util/*.cpp ) | copy_srcs
496498

497499
# Kernel sources needed by at least one shipped profile.
498500
: > "$SCRATCH/kernel_srcs_by_profile.txt"
@@ -504,8 +506,8 @@ done < "$SCRATCH/kernel_union/files.txt"
504506
echo "[3/6] Runtime, extension and kernel sources copied"
505507

506508
# ─────────────────────────────────────────────────────────
507-
# 4. Vendor headers: executorch/** (public), c10/torch,
508-
# flatbuffers, flatcc (+ its C runtime).
509+
# 4. Vendor headers: executorch/** and c10/torch (public),
510+
# flatbuffers (private: only internal headers include it).
509511
# ─────────────────────────────────────────────────────────
510512
copy_headers() { # $1=src dir under ET_ROOT, $2=dest under OUT/include
511513
local src_dir="$1" dest_dir="$2" f
@@ -561,16 +563,9 @@ if [ "$ANY_CADENCE" = true ]; then
561563
copy_headers backends/cadence/generic executorch/backends/cadence/generic
562564
fi
563565

566+
mkdir -p "$OUT_DIR/priv_include"
564567
cp -r "$ET_ROOT/third-party/flatbuffers/include/flatbuffers" \
565-
"$OUT_DIR/include/flatbuffers"
566-
mkdir -p "$OUT_DIR/include/flatcc" "$OUT_DIR/src/flatcc/runtime"
567-
cp "$ET_ROOT/third-party/flatcc/include/flatcc/"*.h "$OUT_DIR/include/flatcc/"
568-
cp -r "$ET_ROOT/third-party/flatcc/include/flatcc/portable" \
569-
"$OUT_DIR/include/flatcc/"
570-
cp "$ET_ROOT/third-party/flatcc/src/runtime/"*.c "$OUT_DIR/src/flatcc/runtime/"
571-
for f in "$OUT_DIR/src/flatcc/runtime/"*.c; do
572-
echo "src/flatcc/runtime/$(basename "$f")" >> "$SCRATCH/core_srcs.txt"
573-
done
568+
"$OUT_DIR/priv_include/flatbuffers"
574569

575570
echo "[4/6] Headers and third-party code copied"
576571

@@ -588,9 +583,7 @@ cp "$ET_ROOT/LICENSE" "$OUT_DIR/LICENSE"
588583

589584
LICENSES="$OUT_DIR/THIRD_PARTY_LICENSES"
590585
mkdir -p "$LICENSES"
591-
for dep in flatbuffers flatcc; do
592-
cp "$ET_ROOT/third-party/$dep/LICENSE" "$LICENSES/$dep-LICENSE.txt"
593-
done
586+
cp "$ET_ROOT/third-party/flatbuffers/LICENSE" "$LICENSES/flatbuffers-LICENSE.txt"
594587
cat > "$LICENSES/README.md" <<'NOTICE'
595588
# Third-party licenses
596589
@@ -599,8 +592,7 @@ ExecuTorch's own BSD license is in the LICENSE file at the root.
599592
600593
| Component | Location in this component | License |
601594
|---|---|---|
602-
| FlatBuffers (Google) | `include/flatbuffers/` | Apache-2.0 — `flatbuffers-LICENSE.txt` |
603-
| flatcc (Mikkel F. Jorgensen) | `include/flatcc/`, `src/flatcc/` | Apache-2.0 — `flatcc-LICENSE.txt` |
595+
| FlatBuffers (Google) | `priv_include/flatbuffers/` | Apache-2.0 — `flatbuffers-LICENSE.txt` |
604596
| PyTorch c10 (Meta) | `include/c10/`, `include/torch/` | BSD-3-Clause, as exact copies from PyTorch core |
605597
606598
`src/profiles/*/register_kernels.cpp` is generated by ExecuTorch's
@@ -674,7 +666,10 @@ KHEAD
674666
help
675667
When disabled, ET_LOG is compiled out entirely
676668
(ET_LOG_ENABLED=0). When enabled, messages go through
677-
ESP_LOG under the "executorch" tag.
669+
ESP_LOG under the "executorch" tag. Newlib's nano printf
670+
(LIBC_NEWLIB_NANO_FORMAT) cannot format the 64-bit integers
671+
in ExecuTorch's error messages, so with it logging is
672+
compiled out regardless, with a build warning.
678673
679674
choice ET_LOG_LEVEL
680675
prompt "ExecuTorch log level floor"
@@ -696,10 +691,11 @@ KHEAD
696691
697692
config ET_OPTIMIZE_SIZE
698693
bool "Build the runtime with -Os"
699-
default n
694+
default y
700695
help
701696
Optimize the ExecuTorch runtime for size regardless of the
702-
project's compiler optimization level.
697+
project's compiler optimization level. This trades speed
698+
for flash; turn it off when inference time matters more.
703699
704700
config ET_PROGRAM_VERIFICATION
705701
bool "Compile in full .pte verification"

‎examples/espressif/idf_component_template/CMakeLists.txt‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ idf_component_register(
5656
"src/platform/esp_pal.cpp"
5757
INCLUDE_DIRS
5858
"include"
59+
PRIV_INCLUDE_DIRS
60+
"priv_include"
5961
PRIV_REQUIRES
6062
${_et_requires}
6163
WHOLE_ARCHIVE
@@ -81,14 +83,13 @@ else()
8183
set(_et_program_verification 0)
8284
endif()
8385

84-
# Consumers including <executorch/...> headers need the same c10 macro mode and
85-
# flatbuffers alignment the runtime was built with. __FUNCTION__ instead of
86-
# __builtin_FUNCTION() keeps log function names without full signatures.
86+
# Consumers including <executorch/...> headers need the same c10 macro mode the
87+
# runtime was built with. __FUNCTION__ instead of __builtin_FUNCTION() keeps log
88+
# function names without full signatures.
8789
target_compile_definitions(
8890
${COMPONENT_LIB}
89-
PUBLIC C10_USING_CUSTOM_GENERATED_MACROS FLATBUFFERS_MAX_ALIGNMENT=1024
90-
ET_USE_BUILTIN_FUNCTION_NAME=0
91-
PRIVATE MAX_KERNEL_NUM=${_et_max_kernel_num}
91+
PUBLIC C10_USING_CUSTOM_GENERATED_MACROS ET_USE_BUILTIN_FUNCTION_NAME=0
92+
PRIVATE FLATBUFFERS_MAX_ALIGNMENT=1024 MAX_KERNEL_NUM=${_et_max_kernel_num}
9293
ET_ENABLE_PROGRAM_VERIFICATION=${_et_program_verification}
9394
)
9495

@@ -100,7 +101,18 @@ if(CONFIG_ET_OPTIMIZE_SIZE)
100101
target_compile_options(${COMPONENT_LIB} PRIVATE -Os)
101102
endif()
102103

103-
if(NOT CONFIG_ET_ENABLE_LOGGING)
104+
# Newlib's nano printf cannot format 64-bit integers, which ExecuTorch's error
105+
# messages print, so logging one could crash the app.
106+
if(CONFIG_ET_ENABLE_LOGGING AND CONFIG_LIBC_NEWLIB_NANO_FORMAT)
107+
message(
108+
WARNING
109+
"ExecuTorch logging is DISABLED: CONFIG_LIBC_NEWLIB_NANO_FORMAT selects "
110+
"newlib's nano printf, which cannot format the 64-bit integers in "
111+
"ExecuTorch's error messages. Use Picolibc (ESP-IDF's default) or turn "
112+
"off nano formatting to keep ExecuTorch logging."
113+
)
114+
endif()
115+
if(NOT CONFIG_ET_ENABLE_LOGGING OR CONFIG_LIBC_NEWLIB_NANO_FORMAT)
104116
target_compile_definitions(${COMPONENT_LIB} PUBLIC ET_LOG_ENABLED=0)
105117
endif()
106118

@@ -126,11 +138,8 @@ target_compile_definitions(
126138
PRIVATE ET_ESP_LOG_LEVEL=${_et_esp_log_level}
127139
)
128140

129-
# ESP-IDF builds with warnings-as-errors; upstream ExecuTorch sources are not
130-
# warning-clean under the IDF flag set, so demote for vendored sources only.
141+
# Vendored ExecuTorch sources use ExecuTorch's own deprecated aliases.
131142
set_source_files_properties(
132-
${_et_vendored_srcs}
133-
PROPERTIES
134-
COMPILE_OPTIONS
135-
"-Wno-error;-Wno-deprecated-declarations;-Wno-unused-parameter;-Wno-sign-compare;-Wno-unused-variable"
143+
${_et_vendored_srcs} PROPERTIES COMPILE_OPTIONS
144+
"-Wno-deprecated-declarations"
136145
)

‎examples/espressif/idf_component_template/idf_component.yml.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## GENERATED by examples/espressif/build_idf_component.sh -- do not edit.
44
## Version is "<ExecuTorch release>~<packaging revision>"; see VERSION
55
## in this directory for the exact commit and op-set hashes. Vendored
6-
## third-party code (FlatBuffers, flatcc: Apache-2.0) is listed in
6+
## third-party code (FlatBuffers: Apache-2.0) is listed in
77
## THIRD_PARTY_LICENSES. No targets list: the runtime is portable
88
## across ESP targets.
99
version: "@ET_COMPONENT_VERSION@"

‎shim_et/xplat/executorch/build/build_variables.bzl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ KERNELS_UTIL_ALL_DEPS_SRCS = [
251251
"kernels/portable/cpu/util/delinearize_index.cpp",
252252
"kernels/portable/cpu/util/distance_util.cpp",
253253
"kernels/portable/cpu/util/dtype_util.cpp",
254+
"kernels/portable/cpu/util/grid_sampler_2d_util.cpp",
254255
"kernels/portable/cpu/util/index_util.cpp",
255256
"kernels/portable/cpu/util/kernel_ops_util.cpp",
256257
"kernels/portable/cpu/util/matmul_ops_util.cpp",
@@ -260,6 +261,7 @@ KERNELS_UTIL_ALL_DEPS_SRCS = [
260261
"kernels/portable/cpu/util/repeat_util.cpp",
261262
"kernels/portable/cpu/util/select_copy_util.cpp",
262263
"kernels/portable/cpu/util/slice_util.cpp",
264+
"kernels/portable/cpu/util/stack_util.cpp",
263265
"kernels/portable/cpu/util/upsample_util.cpp",
264266
]
265267

0 commit comments

Comments
 (0)