arm-gcc compiled code is bigger than on armclang

574 views Asked by At

I've generated the same project on STM32CubeMx and added the same code

  • uint8_t* data = new uint8_t[16]
  • HAL_Delay and HAL_GPIO_TogglePin in infinite loop

for the Keil MDK project and as Makefile project. In both main.c is changed to main.cpp

The whole user code looks like:

    uint8_t* data;
    
    int main(void)
    {
      HAL_Init();
      SystemClock_Config();
      MX_GPIO_Init();
      MX_USART3_UART_Init();
      MX_USB_OTG_FS_PCD_Init();
    
      data = new uint8_t[16];
      for(int i = 0; i < 16; i++)data[i] = i+1;
      
      while (1)
      {
        HAL_GPIO_TogglePin(LD3_GPIO_Port, GPIO_PIN_14);
        HAL_Delay(500);
      }
    }

In Keil I use armclang v6.19 and in Makefile project I use arm-none-eabi-gcc v12.2.1 20221205

I've checked compilation results with different optimization flags and here are results:

  • O0

Keil:

Program Size: Code=11950 RO-data=510 RW-data=12 ZI-data=3012

arm-gcc:

 text    data     bss     dec     hex 
 17572     100    3268   20940    51cc
  • O3

Keil:

Program Size: Code=8238 RO-data=510 RW-data=12 ZI-data=3012  

arm-gcc:

 text    data     bss     dec     hex
 12448     100    3268   15816    3dc8
  • Oz

Keil:

Program Size: Code=6822 RO-data=510 RW-data=12 ZI-data=3012   

arm-gcc:

 text    data     bss     dec     hex
 11876     100    3268   15244    3b8c

What is the reason of such a difference? How can I fix that?

I guess, there are differences in -O* flag meanings in theese compilers and they use different optimizer options, but I am not sure

1

There are 1 answers

0
aelray On

You are comparing two completely different build toolchains so the result will always differ.

Keil

Uses the proprietary armclang to compile and armlink to link. armclang is a fork of clang for which you can see the optimization options here but Arm has it's own documentation for that here because it adds some proprietary magic sauce on top.

Arm GCC

Uses the GNU compiler collection as basis for which you can find the documentation for optimization levels here. There are no 2 documentations for that because Arm actively contributes to the Arm GCC.