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

I3C FAQ for firmware teams covering MIPI I3C bus fundamentals, tooling and adoption in India, from GSAS
Technical Guides Binho Semiconductor Design

I3C FAQ for Firmware Teams: What the Bus Is, What Changes, and What You Need on the Bench

MIPI Alliance describes I3C as the successor to I2C, with legacy compatibility so that I3C and I2C devices can coexist on the same bus, a two-wire interface that supports in-band interrupts to reduce pin count, plus multi-controller support and dynamic addressing. This FAQ answers the questions firmware teams actually ask before adopting it.

31 Jul 2026 · 8 min read
Binho Supernova running in I3C target mode to emulate a device against a customer controller, supported in India by GSAS
Technical Guides Binho Semiconductor Design

Running the Binho Supernova as an I3C Target: Emulating a Device Against Your Own Controller

Binho specifies the Supernova's I3C role as Controller or Target, which means the same adapter can stand in as the device under test rather than only driving one. That second direction is how a team validates its own I3C controller, its ENTDAA implementation and its interrupt handling, before the target silicon exists.

31 Jul 2026 · 7 min read
Functional verification engineer in India reviewing RTL waveforms, coverage charts and a regression dashboard, Siemens Questa One and the Questa Prime to Questa One rename explained by GSAS
Questa Siemens EDA Semiconductor Design

What Happened to Questa Prime and ModelSim?

Siemens has consolidated its Questa verification line under a single brand. The Questa product hub now returns a 301 redirect to the Questa One page, the ModelSim URL redirects to Questa One Sim, and Questa Prime no longer appears on any Siemens public product page. Here is what Questa One actually contains, what Siemens says the word One means, and what Indian verification teams should check before treating this as only a name change.

31 Jul 2026 · 11 min read