Optimization flags arduino 1.6.5

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. I have been reading this:
http://www.instructables.com/id/...

 

2. Page 6 states that platform.txt exist in:
C:\Users\XXXX\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\1.6.x\platform.txt

However I have found that inside mu PC;   C:\Users\XXXX\AppData\Roaming\Arduino15 , there is no folder name packages. I have searched in hidden files also.

 

3. However I have found platform.txt file in:
C:\Program Files\Arduino\hardware\arduino\avr

Not sure if I have to edit in this. I have attached a copy.

 

4. There are three places where -Os is found, line 23,26 & 30.
So should I change at these 3 places only depending on requirement:
No optimization: -O0
speed optimization: -O3
size optimization: -Os
balance optimization: -O2

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The important thing about optimization is "don't bother until you have to". If you find some part of your code that is time critical (or space critical) and you need to do something about it then worry about optimizing just that section.

 

Note that since about 4.6 the GCC compiler has now added:

 

https://gcc.gnu.org/onlinedocs/g...

 

So on a function by function basis you can now:

#pragma GCC push_options
#pragma GCC optimize ("O3")

void foo(void) {
    // this function benefits particularly from -O3 optimization (say)
}
#pragma GCC pop_options

void bar(void) {
    // this function just inherits -Os, -O1 or whatever the code is
    // built with.
}

Hopefully the usage here is obvious? So I wouldn't bother about the overall Arduino build option - the Arduino code is far from efficient anyway - but if some part of the code is specifically time/space critical then use these #pragma's around the function(s) you want to apply special build options to.

#pragma GCC push_options
#pragma GCC optimize ("O0")

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without -fwhole-program* flag I would only bother about that -Os doesnt affect for code accesing each other, across separate compilation units (each .c / .cpp / .ino files).

Last Edited: Mon. Aug 24, 2015 - 05:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson, I have tried this option, but below warning comes:

 

sketch_aug25a.ino:224:9: warning: #pragma GCC target is not supported for this machine [-Wpragmas]
 

 

@jnk0le, how to use it in platform.txt. There is no such word -fwhole-program, in platform.txt.

Since I have multiple files in my code, so should I replace "-Os" with "-fwhole-program -O3"?

 

 

 

Last Edited: Tue. Aug 25, 2015 - 09:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson, I have tried this option, but below warning comes:

 

sketch_aug25a.ino:224:9: warning: #pragma GCC target is not supported for this machine [-Wpragmas]

It's odd isn't it? I tried this:

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>type test.c
#include <avr/io.h>
#pragma GCC push_options
#pragma GCC optimize ("O3")
int main(void) {
        PORTB |= 0x20;
}
#pragma GCC pop_options

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>avr-gcc -mmcu=atmega328 -O0 -g test.c -o test.elf
test.c:7:9: warning: #pragma GCC target is not supported for this machine [-Wpragmas]
 #pragma GCC pop_options
         ^

But notice it only warns about "pop_options" but not the first two! If I change pop_options to reset_options:

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>type test.c
#include <avr/io.h>
#pragma GCC push_options
#pragma GCC optimize ("O3")
int main(void) {
        PORTB |= 0x20;
}
#pragma GCC reset_options

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>avr-gcc -mmcu=atmega328 -O0 -g test.c -o test.elf

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>

It builds without warning. So it looks like an error in the handling of "pop_options". What's more:

E:\Program Files\arduino-1.6.3\hardware\tools\avr\bin>avr-objdump.exe -S test.elf

test.elf:     file format elf32-avr

Disassembly of section .text:

00000000 <__vectors>:
   0:   0c 94 34 00     jmp     0x68    ; 0x68 <__ctors_end>
   4:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>

        [SNIP]

  74:   0e 94 40 00     call    0x80    ; 0x80 <main>
  78:   0c 94 42 00     jmp     0x84    ; 0x84 <_exit>

0000007c <__bad_interrupt>:
  7c:   0c 94 00 00     jmp     0       ; 0x0 <__vectors>

00000080 <main>:
#include <avr/io.h>
#pragma GCC push_options
#pragma GCC optimize ("O3")
int main(void) {
        PORTB |= 0x20;
  80:   2d 9a           sbi     0x05, 5 ; 5
}
  82:   08 95           ret

That has compiled the PORTB access to a single SBI opcode so clearly the code was built -O3 and not the -O0 on the command line!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not sure how its done in arduino but I would try looking for "other optimization flags" in preporties or just add it to makefile.

 

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

The only thing what -fwhole-program does, is just merging all files into one.

 

If you have function like this in a separate file

void foo(int in)
{
	switch(in)
	{
		case 0: LED1_TOG(); break;
		case 23: LED9_TOG(); break;
		case 666: BEEP();
	}
}

GCC will compile all cases in this function even if you are calling it from main.cpp once with argument known during compilation.

 

Thats why DigitalWrite() is 20 times slower than direct port acces.

 

 

Do not use -O3 on avr. It cannot do nothing more than unrolling loops. O3 is to provide code vectorization for Hi-end ARMs and x86 (SSE/NEON instructions, deep pipelines etc.)

 

EDIT: On newer gcc version -flto should be used instead of -fwhole-program.

 

 

Last Edited: Tue. Aug 25, 2015 - 07:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not sure how its done in arduino but I would try looking for "other optimization flags" in preporties or just add it to makefile.

Arduino doesn't have either "properties" or "makefiles", which is what makes this an interesting question.

 

In general, I've found the gcc optimization flags to be "not intuitive" for avr-gcc, because the final code is "too different" from the intermediate code that is examined for optimization.

(-Os routinely makes bad decisions, -O3 tends to cause a lot of bloat by inlining functions that are a lot bigger than avr-gcc seems to think.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Final Code size by opt settings:

1. Default -Os
Sketch uses 22502 bytes (8%) of program storage space. Maximum is 253952 bytes.
Global variables use 674 bytes (8%) of dynamic memory, leaving 7518 bytes for local variables. Maximum is 8192 bytes.

 

2. Same code size generated for below thre options
    a) Replace -Os by -O3
    b) Replace -Os by -fwhole-program -O3
    c) Replace -Os by -flto -O3
Sketch uses 44858 bytes (17%) of program storage space. Maximum is 253952 bytes.
Global variables use 686 bytes (8%) of dynamic memory, leaving 7506 bytes for local variables. Maximum is 8192 bytes.

Havent tested on hardware yet. Will check. ordered new finger print module.

Last Edited: Sat. Sep 9, 2017 - 07:43 AM