Skip to content

build(lto): enable link time optimization for F051#12

Open
dakejahl wants to merge 1 commit into
mainfrom
build/enable-lto
Open

build(lto): enable link time optimization for F051#12
dakejahl wants to merge 1 commit into
mainfrom
build/enable-lto

Conversation

@dakejahl

@dakejahl dakejahl commented Jun 22, 2026

Copy link
Copy Markdown

Summary

Compile and link the F051 firmware with -flto. Scoped to F051 only via LTO_FLAGS_F051; all other MCUs are unchanged (verified: G431 flash is unaffected apart from +4 B for the used attribute below). Should merge after the volatile PR so cross-TU inlining is safe.

Problem

F051 is the most CPU-constrained target. Without LTO the compiler cannot inline across translation units, leaving call overhead in the hot commutation and PWM paths on the Cortex-M0.

Solution

Enable -flto for F051 only — the whole firmware is built in a single compiler invocation, so this needs no other build changes. The filename string gets __attribute__((used)) so LTO does not drop it from its section. A defensive CAN guard keeps LTO off any future DroneCAN F051 build (none exists today).

⚠️ Flash impact — read before merging. At -O3, LTO inlining grows F051 flash from 22200 B (81%) to 26680 B (97.3%, 744 B free) on its own, while cutting RAM (3688 → 2824 B via dead-code elimination). It fits, but standalone headroom is thin. It does not overflow when combined with the other F051 PRs — measured: LTO + the RAM-functions PR is 26024 B (94.9%), and the full F051 stack including the flash-reclaim PRs (gate custom tune, drop the SysTick handler) lands at 24980 B (91%). The thin standalone margin is the thing to watch; keep the reclaim PRs in the stack. Given 97.3% on its own, consider treating this as risk:4.

Compile and link the F051 firmware with -flto. The whole firmware is
built in a single compiler invocation, so cross translation-unit inlining
needs no other build changes, and it matters most on the cortex-m0 where
call overhead is high and flash is tight.

Scoped to F051 only for now (LTO_FLAGS_F051) rather than firmware-wide:
F051 is the target that needs it, and keeping LTO off the other MCUs
avoids widening the test surface while it is evaluated on hardware. Other
targets can opt in with LTO_FLAGS_<MCU> := -flto. The filename string gets
__attribute__((used)) so LTO cannot drop it from its section. A defensive
CAN guard keeps LTO off any future DroneCAN F051 build (none exists today).
@dakejahl dakejahl added risk:3 Moderate — behavior/timing change; review carefully risk:4 High — invasive; needs careful review + HW testing wontfix This will not be worked on and removed risk:3 Moderate — behavior/timing change; review carefully labels Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:4 High — invasive; needs careful review + HW testing wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant