Skip to main content
Compiler hints and tips on using the optimization levels smartly in Keil MDK/DS toolset., featured image

Compiler hints and tips on using the optimization levels smartly in Keil MDK/DS toolset.

GSAS Engineering · · 4 min read

The Arm Compiler optimizes your code for small code size and high performance. The trade-off between binary footprint and execution speed is one of the most consequential settings on a Cortex-M project, and the right answer depends on which build you’re producing.

Arm Compiler 6 optimization levels at a glance

Arm Compiler 6 (armclang) is the LLVM-based toolchain that ships with Keil MDK and Arm Development Studio. It exposes six optimization levels, each tuned for a different point on the size/speed/debuggability curve. Pick the level that matches the job of the build, not a vague preference for “faster code.”

LevelWhen to useTrade-off
-O0Debug builds, single-step trace, breakpoint-heavy bring-upLargest binary, slowest runtime, full source-line fidelity
-O1Light optimization with most debug info preservedSome local variables become unavailable in the debugger
-O2Default for releaseBest general-purpose balance of speed and size
-O3Performance-critical loops, vectorizable DSP kernelsLarger binary; aggressive inlining can hurt I-cache hit rates on Cortex-M7
-OsCode-size optimized while keeping reasonable speedSlightly slower than -O2 on hot paths
-OzSmallest possible binary, e.g. Cortex-M0+ with 32 KB flashMost aggressive size optimization; can disable inlining that -Os keeps

Source: Arm Compiler armclang Reference Guide, -O options and the Arm Compiler 6 User Guide.

Function-level optimization with __attribute__

A single -O flag on the project applies to every translation unit. That is rarely the right answer for firmware, you usually want one or two hot functions tuned aggressively while the rest of the image stays size-optimized. armclang accepts the GCC-compatible __attribute__((optimize("O3"))) on individual functions, and #pragma clang optimize on/off for bracketed regions:

__attribute__((optimize("O3")))
void fir_filter(const int16_t *in, int16_t *out, size_t n) {
    /* hot DSP loop, compile at O3 even if the project is -Oz */
}

This pattern keeps the global build at -Os or -Oz for footprint while letting compute kernels run at -O3.

When -Oz pays off and when it hurts

-Oz is the right default for memory-constrained MCUs, Cortex-M0+ parts with 32–64 KB flash, BLE peripherals, sensor nodes. On a Cortex-M7 running an FFT or motor-control loop, -Oz will suppress the inlining and loop unrolling those kernels depend on, and you will measure the difference on a scope. Use -Oz globally with per-function __attribute__((optimize("O3"))) overrides on the kernels that matter, rather than flipping the whole project to -O3.

Linker dead-code elimination pairs with the compiler choice

Compiler optimization only goes as far as the translation unit. To strip unused functions and data at link time, compile with -ffunction-sections -fdata-sections so each symbol lands in its own section, then link with armlink --remove (or armclang -Wl,--gc-sections when invoking the linker via the compiler driver). The Arm linker’s unused section elimination walks the call graph from the entry point and discards anything unreachable. This pairing, fine-grained sections at compile time, garbage collection at link time, typically recovers 5–15 % of image size on a real firmware build before you change a single line of C.

For end-to-end toolchain details and the latest armclang release notes, see the Arm Compiler 6 product page and the Keil MDK page on developer.arm.com.

LEARN MORE ABOUT Arm Tools

Also appears in:

Interested in Arm tools?

Talk to our application engineers for personalized tool recommendations.

Stay in the Loop

Get monthly compliance updates, product insights, and engineering best practices delivered to your inbox.

Related Articles

HyperLynx AMS schematic in Xpedition Designer above a system model containing an FMI block, from Siemens EDA's FMI co-simulation article
PCB Design Siemens EDA

FMI Co-Simulation in HyperLynx AMS and PartQuest Explore: Electrical, Mechanical and Thermal Models in One Run

Since the 2510 release, HyperLynx AMS exports and imports FMI Functional Mock-up Units, so a schematic-driven circuit model can co-simulate with models from other FMI-compatible tools, such as a Simcenter Amesim mechanical model. What the two FMU types mean and where it pays off for Indian hardware teams.

4 Nov 2026 · 5 min read
Solid Sands campaign artwork for SuperTest Van Gogh 6.0, C++ source code set against Van Gogh's The Starry Night
Functional Safety Solid Sands

SuperTest Van Gogh 6.0: C23, C++23 and the C++20 Modules Gap

Solid Sands has released SuperTest Van Gogh 6.0 with more than 1,900 new tests, including 480 for C23 and 440 for C++23. The number worth your attention is a different one: GCC, Clang and MSVC each currently fail about 20% of its 173 C++20 modules tests.

6 Oct 2026 · 7 min read
The bench to harness adapter path drawn left to right: the ECU connector, a test lead, a media converter or pluggable T1 module, RJ45, and the host, showing where each connector family sits between the device under test and the laptop, from GSAS Micro Systems India
Automotive Ethernet Automotive & Mobility

Automotive Ethernet Connectors: H-MTD, MATEnet, MQS

An ECU arrives on the bench with a connector nobody has a mate for, and the day is gone. Search the family names and you get connector product pages that describe their own part and stop there. This article puts the families side by side in one table using only what their public pages state, then makes the point those pages leave out: the IEEE link segment definition, not the connector, is what sets reach and loss limits, and shielding is a channel decision rather than a preference. Written by the GSAS Micro Systems engineering team in India.

29 Aug 2026 · 12 min read