Repository navigation
feat(shrink): apply --shrink=printf on ESP32 via newlib-nano (#1459) - #1826
Merged
Merged
Conversation
`--shrink=printf` now reaches the link stage on ESP32: the link gains `-specs=nano.specs` (which rewrites -lc/-lm to libc_nano/libm_nano) plus `-Wl,-u,_printf_float` so `%f` keeps working. The full `_vfprintf_r` (11,413 B) + `_svfprintf_r` (11,222 B) cluster is replaced by newlib's own thin formatter (698 B / 624 B), so no printf semantics are invented in-tree. Blink on esp32-s3-devkitc-1 (xtensa-esp-elf 14.2.0, IDF 5.5.5): flash 356,075 -> 333,047 B (-23,028, -6.5%), .text 229,743 -> 208,227. - `shrink: ShrinkMode` on BuildParams, threaded CLI -> daemon -> orchestrator (the daemon does not inherit caller flags), like `caller_path`. - `probe_nano_specs()` asks the selected compiler for its nano.specs and requires an absolute existing path; without it the build logs and links unshrunk instead of failing on a missing spec file. - `Esp32FingerprintMetadata.shrink_mode` invalidates the fast path when the mode flips, so off <-> printf always relinks. - `--shrink=printf` on ESP32 reports the applied reducer instead of the "no effect yet" warning; every other mode/platform keeps the warning.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements the one reducer #1459 asks for on ESP32-S3/Xtensa + newlib:
fbuild build --shrink=printfnow links newlib-nano instead of the full newlib printf cores, and says so on the terminal.Part 1 (#1813, merged 3c2b0a2) made
--shrinkreport that nothing was registered. This PR is the reducer itself.Why newlib-nano and not a hand-written printf
Every newlib formatting entry point funnels through
_vfprintf_r/_svfprintf_r. Rather than re-implementprintf(the #493--wrapdesign), the Xtensa toolchain already ships a second, vendor-maintained copy of the same libc —libc_nano.a+nano.specs— whose spec file rewrites-lc→-lc_nanoand-lm→-lm_nanoat link time. So the substitution is Espressif's own thin formatter: no formatting semantics are invented in this repo, and--gc-sectionsstill applies.-Wl,-u,_printf_floatis linked alongside it, because newlib-nano gates its float formatter behind that symbol and otherwise prints%fverbatim. Keeping it means a shrunk build formats the same values; it costs the float path (~1.5 KB) but not the 11 KB full_vfprintf_r.Measured A/B (Blink,
tests/platform/esp32s3, ESP32-S3, xtensa-esp-elf 14.2.0, IDF 5.5.5)Same link argv,
-specs=nano.specs -Wl,-u,_printf_floatadded:--shrink=printf.text.data_vfprintf_r_svfprintf_r_dtoa_rWithout
-u _printf_floatthe same link is 276,282 B of flash (_dtoa_rdrops too) — −31,436 B — but%fstops printing. That is the knob to revisit if a project never formats floats; it is not the default.What changed
crates/fbuild-build-esp/src/esp32/printf_thin.rs(new, 116 LOC):applies()(explicitprintf+ ESP32 only),link_flags(),nano_specs_from_probe()(pure, tested) andprobe_nano_specs(), which asks the selected compiler-print-file-name=nano.specsand requires an absolute path that exists.shrink: --shrink=printf found no nano.specs …; building unshrunkand links normally — it cannot break a build.BuildParams.shrink: ShrinkMode(threaded CLI → daemon request → orchestrator, likecaller_path), because the daemon does not inherit caller flags; the ESP32 orchestrator extendsLinkExtraArgs.flagsonly whenapplies()is true.Esp32FingerprintMetadata.shrink_mode, so flippingoff↔printfinvalidates the fast path and relinks (test added).shrink_notice:--shrink=printfon ESP32 reports the applied reducer line instead of the "no effect yet" warning; every other platform/mode keeps the warning (tests updated).ShrinkMode::as_str/FromStrfor the daemon request field.Tests
bash test(workspace) → 4116 passed, 0 failed, rc 0 (TMPDIR=/home/niteris/t).cargo test -p fbuild-build-esp --lib printf_thin→ 3 passed.cargo test -p fbuild-build-engine --lib shrink→ 42 passed.cargo test -p fbuild-cli --bin fbuild shrink→ 16 passed, incl. the updated notice tests.cargo clippy --workspace --all-targets -- -D warnings→ rc 0.cargo check -p fbuild-build-esp -p fbuild-cli -p fbuild-daemon --all-targetsclean.ci/enforce_platform_boundary.py --print-totals→ 44/43;ci/check_test_layout.pyok.The LOC gate first failed on
tests/it/esp32_build.rs(994 → 1004 lines because everyBuildParamsliteral needed the new field); the second commit replaces its nine duplicated literals with oneesp32_params()builder (file is now 819 lines).Not in this PR
build_info_<env>.jsontelemetry (ShrinkRecordstill unwired) andAuto → Safepromotion.--shrink=autocontinues to resolve toOff, as documented, until the formatting-compatibility matrix is justified; the reducer is explicit and opt-in.Live CLI A/B (
tests/platform/esp32s3, same dir, mode flipped)nmon the shrunk image:_vfprintf_r698 B,_svfprintf_r624 B,_printf_float1120 B,_dtoa_runchanged. The RAM figure is identical (74919 B) in both — the reducer only touches flash.Not verified: a flash/boot of the shrunk image. The attached USB-JTAG device reports only the shared
303A:1001Espressif identity, so flashing it could have targeted the wrong chip. The link, symbol and image-size deltas above are real; boot is unproven.Refs #1459 — the remaining acceptance item is
build_info_<env>.jsontelemetry (ShrinkRecordis still unwired) and anyAuto → Safepromotion.