Skip to content

perf(avr): -mcall-prologues, -1022 B atmega8 flash - #185

Merged
Andste82 merged 1 commit into
mainfrom
perf/avr-call-prologues
Oct 2, 2026
Merged

Andste82 merged 1 commit into
mainfrom
perf/avr-call-prologues

Conversation

@Andste82

@Andste82 Andste82 commented Oct 2, 2026

Copy link
Copy Markdown
Member

AVR is the tightest row in the footprint tables and the one place where a further -Os knob still pays. -mcall-prologues routes every function's register-save prologue and epilogue through libgcc's shared __prologue_saves__ / __epilogue_restores__ pair instead of emitting both inline.

Measured

tools/footprint.sh, plus a linked image to net out the helper pair:

atmega8 .text before after Δ
Full 8572 B 7550 B −1022 B, −11.9 %
Full, strict UTF-8 9028 B 7954 B −1074 B
Minimal 2840 B 2438 B −402 B, −14.2 %
Minimal, without object.c 1952 B 1678 B −274 B, −14.0 %
linked image 8980 B 8068 B −912 B

The 110 B gap between archive and image is that helper pair, which an image pays once and a firmware built with the same flag amortises. The cost is cycles — the trade this library makes on these parts: flash is the scarce resource, speed is subordinate.

Placement

Next to the other size flags in the top-level CMakeLists.txt rather than in utils/avr/toolchain-avr.cmake, so a consumer who brings their own AVR toolchain file still gets it. Guarded on CMAKE_SYSTEM_PROCESSOR because the flag is AVR-only. naked functions (utils/avr/startup.c) emit no prologue and are unaffected.

The equivalents elsewhere were measured and deliberately not set

  • RISC-V -msave-restore — 308 B in the archive, but only 56 B in a linked image: the __riscv_save_*/__riscv_restore_* set costs 1284 B there. Only worth it if the whole firmware carries the flag.
  • ARMv6-m — nothing left to take: -fno-jump-tables (+20 B), -fno-inline-functions-called-once (+16 B), and -ftree-tail-merge, -fconserve-stack, -fno-tree-switch-conversion, -fipa-pta, --param=inline-unit-growth=0 (all ±0 B).

README

All four atmega8 rows regenerated, the hold-back reference re-measured (512 B → 398 B on atmega8), and the flag documented — the tables no longer reproduce from a plain -Os build, and that is exactly the kind of drift utils/ci/build-config.sh exists to prevent.

Verification

  • Non-AVR bit-identical: ARMv6-m 3960 B, ARMv7-m 3960 B, RV32IMC 5080 B, and all twelve ARM per-switch rows unchanged.
  • Hosted: builds under -Werror, 5/5 ctest green.
  • AVR default MCU (atmega328p, no -DAVR_MCU) builds.
  • The AVR unit-test image now overflows FLASH by 21232 B instead of 44165 B — still does not fit, so that workflow stays on manual dispatch, but the gap is halved.
  • The AVR all target fails identically with and without the flag (Catch2 needs <chrono>, which avr-libc does not have) — pre-existing and flag-independent.

Not touched

Two README footprint figures are unaffected by this flag but were not re-verified here, and may have drifted from the tagged-union work: the "about 375 B of ARMv6-m .text" skip-code claim, and the per-TU C++ wrapper table (338 / 834 / 884 / 1034 B). Neither has a harness that regenerates it.

🤖 Generated with Claude Code

AVR is the tightest row in the footprint tables and the one place where a
further -Os knob still pays. -mcall-prologues routes every function's
register-save prologue and epilogue through libgcc's shared
__prologue_saves__ / __epilogue_restores__ pair instead of emitting both
inline.

Measured with tools/footprint.sh, and in a linked image to net out the
helper pair:

  Full                 8572 B -> 7550 B  (-1022 B, -11.9%)
  Full, strict UTF-8   9028 B -> 7954 B  (-1074 B)
  Minimal              2840 B -> 2438 B  ( -402 B, -14.2%)
  Minimal, no object.c 1952 B -> 1678 B  ( -274 B, -14.0%)
  linked image         8980 B -> 8068 B  ( -912 B)

The 110 B gap between archive and image is that helper pair, which an image
pays once and a firmware built with the same flag amortises. The cost is
cycles, which is the trade this library makes on these parts: flash is the
scarce resource, speed is subordinate.

It lives next to the other size flags in the top-level CMakeLists rather
than in utils/avr/toolchain-avr.cmake, so a consumer who brings their own
AVR toolchain file still gets it, and is guarded on CMAKE_SYSTEM_PROCESSOR
because the flag is AVR-only. `naked` functions (utils/avr/startup.c) emit
no prologue and are unaffected.

The equivalents on the other targets were measured and deliberately not
set: RISC-V -msave-restore wins 308 B in the archive but only 56 B in a
linked image, because the __riscv_save_*/__riscv_restore_* set costs 1284 B
there; on ARMv6-m -fno-jump-tables (+20 B), -fno-inline-functions-called-once
(+16 B), -ftree-tail-merge, -fconserve-stack, -fno-tree-switch-conversion,
-fipa-pta and --param=inline-unit-growth=0 (all +-0 B) buy nothing.

README: all four atmega8 rows regenerated, the hold-back reference
re-measured (512 B -> 398 B on atmega8), and the flag documented, since the
tables no longer reproduce from a plain -Os build. Every non-AVR figure is
bit-identical, and the hosted suite stays green (5/5).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Andste82
Andste82 merged commit 4754ec8 into main Oct 2, 2026
26 checks passed
@Andste82
Andste82 deleted the perf/avr-call-prologues branch October 2, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant