From 02ea3aefb8e1fecf7c32f4fec166d52e41a5eff5 Mon Sep 17 00:00:00 2001 From: Andste82 Date: Fri, 2 Oct 2026 09:51:55 +0000 Subject: [PATCH] perf(avr): -mcall-prologues, -1022 B atmega8 flash 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) --- CMakeLists.txt | 27 +++++++++++++++++++++++++++ README.md | 22 +++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c622b1..e286f32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,33 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") add_compile_options(-ffunction-sections -fdata-sections) add_link_options(-Wl,--gc-sections -Wl,-Map=mapfile.map) + # AVR is the tightest target in the footprint tables, and the one place + # where a further -Os knob still pays: -mcall-prologues replaces each + # function's register-save prologue and epilogue with a call to the shared + # __prologue_saves__ / __epilogue_restores__ pair in libgcc. Measured on + # atmega8, full configuration (tools/footprint.sh, and a linked image to net + # out the helpers): + # + # libsofabuffers.a 8572 B -> 7550 B (-1022 B) + # linked image 8980 B -> 8068 B ( -912 B) + # + # The 110 B difference is that helper pair, which the image pays once; a + # firmware built with the same flag amortises it and keeps the full -1022 B. + # The cost is cycles -- every call now enters and leaves through a shared + # routine -- which is the trade this library makes deliberately: flash is the + # scarce resource on these parts, speed is subordinate. + # + # Guarded on the processor rather than the compiler because the flag is + # AVR-only; CMAKE_SYSTEM_PROCESSOR is set by utils/avr/toolchain-avr.cmake. + # `naked` functions (utils/avr/startup.c) emit no prologue and are + # unaffected. Other architectures measured no gain from their equivalents: + # 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, + # and on ARMv6-m every comparable knob came out at ±0 B or worse. + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "avr") + add_compile_options(-mcall-prologues) + endif() + if(SOFAB_ENABLE_LTO) # -O3 rather than the -Os above: this leg is not measuring footprint, it # is trying to give the optimiser every reason to act on a type-based diff --git a/README.md b/README.md index 20c1751..bdbaf2f 100644 --- a/README.md +++ b/README.md @@ -713,6 +713,18 @@ is `.text` (flash). The tables are `size libsofabuffers.a` at `-Os`, regenerated configuration; `atmega8` is the exception, its workflow being on manual dispatch until the test image links again. +The AVR rows carry one flag beyond `-Os`: `-mcall-prologues`, which routes every +prologue and epilogue through libgcc's shared save/restore pair. It is worth +1022 B of the `atmega8` *Full* row and is applied by the top-level +`CMakeLists.txt` for that processor only, so a hand-rolled AVR build without it +measures about 1 KB more. In a linked image it nets 912 B — the shared +pair costs 110 B once, which a firmware built with the same flag amortises. +It buys flash with cycles, which is the trade this library makes on these parts. +The equivalents elsewhere do not pay and are deliberately not set: RISC-V +`-msave-restore` saves 308 B in the archive but only 56 B in an image +(its helper set costs 1284 B there), and on ARMv6-m every comparable knob +measured ±0 B or worse. + **Full configuration** | Architecture | .text | .data | .bss | @@ -720,7 +732,7 @@ until the test image links again. | ARMv6-m | ~3.9KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~3.9KB | 0.0KB | 0.0KB | | RV32IMC | ~5.0KB | 0.0KB | 0.0KB | -| atmega8 | ~8.4KB | 0.0KB | 0.0KB | +| atmega8 | ~7.4KB | 0.0KB | 0.0KB | **Full configuration, strict UTF-8 on** — the only rows where the validator (`utf8.c`) is compiled in. The delta over *Full* above is its entire @@ -731,10 +743,10 @@ until the test image links again. | ARMv6-m | ~4.1KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~4.1KB | 0.0KB | 0.0KB | | RV32IMC | ~5.2KB | 0.0KB | 0.0KB | -| atmega8 | ~8.8KB | 0.0KB | 0.0KB | +| atmega8 | ~7.8KB | 0.0KB | 0.0KB | The [hold-back framing](#sequence-framing-and-the-hold-back-window) is part of -those *Full* rows: 276 B on ARMv6-m, 512 B on atmega8. A pure-C consumer +those *Full* rows: 276 B on ARMv6-m, 398 B on atmega8. A pure-C consumer encoding only through `sofab_object_encode()` takes it back out with `SOFAB_DISABLE_LAZY_SEQ_SUPPORT` — ARMv6-m returns to 3684 B, `sofab_ostream_t` shrinks from 56 B to 20 B per stream, and a typical encode drops @@ -750,7 +762,7 @@ unaffected. | ARMv6-m | ~1.1KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~1.1KB | 0.0KB | 0.0KB | | RV32IMC | ~1.4KB | 0.0KB | 0.0KB | -| atmega8 | ~2.8KB | 0.0KB | 0.0KB | +| atmega8 | ~2.4KB | 0.0KB | 0.0KB | Same minimal configuration, additionally without `object.c` (`SOFAB_DISABLE_OBJECT_API`): @@ -760,7 +772,7 @@ Same minimal configuration, additionally without `object.c` | ARMv6-m | ~0.7KB | 0.0KB | 0.0KB | | ARMv7-m+fp.dp | ~0.7KB | 0.0KB | 0.0KB | | RV32IMC | ~0.9KB | 0.0KB | 0.0KB | -| atmega8 | ~1.9KB | 0.0KB | 0.0KB | +| atmega8 | ~1.6KB | 0.0KB | 0.0KB | #### What each switch is worth