MikeMelbourne
Posts: 5
Joined: Mon Apr 11, 2016 10:55 pm

Best Compilation Flags for RPi3 on Raspbian 32 bit?

Mon Apr 11, 2016 11:20 pm

Hi Everyone,

Firstly, I apologise if this has been asked before. I have tried to use the search function for matches of "cortex-a53" but got no matches.

I have a Raspberry Pi 3 (running Raspbian) and I'm wanting to compile RetroPie (emulation/gaming) components to take full advantage of the available instruction sets on the RPi 3.

The RetroPie setup script has the following flags pre-configured for RPi 3:

Code: Select all

-O3 -mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard -funsafe-math-optimizations
I'm have gcc 4.9.2 and I've had a bit of a read of https://gcc.gnu.org/onlinedocs/gcc-4.9. ... tions.html however it's rather over my head. I'm guessing that gcc doesn't accurately determine the best choice of flags for the RPi 3 if I get the following:

Code: Select all

gcc -mcpu=native -march=native -Q --help=target
*** Error in `gcc': double free or corruption (top): 0x00365c18 ***
After some Googling, I've found the following Gentoo page where a user had settled on:

Code: Select all

-march=armv8-a+crc -mtune=cortex-a53 -mfpu=crypto-neon-fp-armv8 -mfloat-abi=hard -ftree-vectorize -funsafe-math-optimizations -O2 -pipe
So does anyone have a definitive answer on the best possible (most performant) compiler flags for 32 bit Raspbian on the RPi 3? It might stop my descent into madness! :-)

Thanks in advance,

Mike

ejolson
Posts: 3563
Joined: Tue Mar 18, 2014 11:47 am

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 12:53 am

MikeMelbourne wrote:

Code: Select all

-O3 -mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard -funsafe-math-optimizations

Code: Select all

-march=armv8-a+crc -mtune=cortex-a53 -mfpu=crypto-neon-fp-armv8 -mfloat-abi=hard -ftree-vectorize -funsafe-math-optimizations -O2 -pipe
As either of the recommended options seem pretty similar, I'd be surprised of there was much difference in the generated code.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 1:14 am

Try compiling "hello, world!" (from the source below, its a little different) using the following:

gcc -v -Wall -o hello hello.c

Code: Select all

#include 
#include 

#define HELLO 1
#define HOLA 2
#define HOWDY 3
#define HEY 4
#define HI 5

int parseArgZero(char** args);

int main(int argc, char** argv) {
    switch (parseArgZero(argv)) {
      case HI:
         printf("\n'ey, babe'!\n\n");
         break;
      case HEY:
         printf("\nhey, yourself!\n\n");
         break;
      case HOWDY:
         printf("\nhey howdy hey, y'all!\n\n");
         break;
      case HELLO:
         printf("\nhello, world!\n\n");
         break;
      case HOLA:
         printf("\nhola, mundo!\n\n");
         break;
      default:
         printf("\nhow, rude!\n\n");
         ;
    }
    return 0;
}

int parseArgZero(char** args) {
   int rc = 0;
   char* hello = "hello";
   char* hola = "hola";
   char* howdy = "howdy";
   char* hey = "hey";
   char* hi = "hi";
   if(strcmp(hello, args[0])==0)
      rc = 1;
   if(strcmp(hola, args[0])==0)
      rc = 2;
   if(strcmp(howdy, args[0])==0)
      rc = 3;
   if(strcmp(hey, args[0])==0)
      rc = 4;
   if(strcmp(hi, args[0])==0)
      rc = 5;
   return rc;
}

The -v will allow you to see all of the compiler options which were automatic. I suspect you're trying to overthink the raspbian | debian guys who have setup gcc to be a fantastic optimizing compiler already; based on what is included in your code, it does pretty much the right thing already.

My "hello, world!" app can be called from various symbolic links:

ln -sf /usr/local/bin/hello hola
ln -sf /usr/local/bin/hello howdy


... depending on how it was called the parm zero is used to have "hello" give the right results... works similarly to busybox...

... I digressed a bit <sorry> the main point is that you probably are not going to make your compiles any more efficient nor optimal than the raspbian guys have already done for you (for the most part).
marcus
:ugeek:

MikeMelbourne
Posts: 5
Joined: Mon Apr 11, 2016 10:55 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 1:59 am

MarkHaysHarris777 wrote: The -v will allow you to see all of the compiler options which were automatic. I suspect you're trying to overthink the raspbian | debian guys who have setup gcc to be a fantastic optimizing compiler already; based on what is included in your code, it does pretty much the right thing already.

the main point is that you probably are not going to make your compiles any more efficient nor optimal than the raspbian guys have already done for you (for the most part).
Thanks for replying, however I'm not sure I agree.

gcc on Raspbian is not aware of the specifics of the RPi 3 ARM chip. In fact, it can't even detect the specific type of chip when executing

Code: Select all

gcc -mcpu=native -march=native -Q --help=target
without throwing an error. Even if it didn't generate an error, it could possibly mean it was compiling with some lower denominator ARM base instruction set (kinda like compiling a 386 binary on a Pentium).

I believe Rasbian binaries are compiled for a base instruction set compatabile with RPi 1, RPi 2, RPi 3 and RPi Zero - it can do this without knowing anything new/fancy available to the RPi 3.

MikeMelbourne
Posts: 5
Joined: Mon Apr 11, 2016 10:55 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 4:23 am

cat /proc/cpuinfo shows

Code: Select all

Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
I guess I can look up these, and see if there are any equivalent flags to enable:

https://gcc.gnu.org/onlinedocs/gcc-4.9. ... tions.html

I guess this is as good a place as any to start! :)

MikeMelbourne
Posts: 5
Joined: Mon Apr 11, 2016 10:55 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 5:33 am

OK, I've come up with the following:

Code: Select all

-O2 -Otime
-marm
-mabi=aapcs-linux
-march=armv8-a+crc
-mfloat-abi=hard
-mfpu=neon-fp-armv8 #other options are 'fp-armv8', 'neon-fp-armv8', and 'crypto-neon-fp-armv8'
-funsafe-math-optimizations
-mhard-float
-mlittle-endian
-mtls-dialect= gnu2 #or just gnu if gnu2 doesn't work
-mtune=cortex-a53
-munaligned-access
-mvectorize-with-neon-quad
#-ftree-vectorize (removed since Autovectorizing (-ftree-vectorize) for NEON gives between zero and negligible performance gains)
Hopefully I'll get some time shortly to try it out. Thanks for your input guys. :)

jahboater
Posts: 4681
Joined: Wed Feb 04, 2015 6:38 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 12:19 pm

In summary, I now have:-
Pi3

Code: Select all

-mcpu=cortex-a53  -mfpu=neon-fp-armv8
Pi2

Code: Select all

-mcpu=cortex-a7  -mfpu=neon-vfpv4
Pi1

Code: Select all

-mcpu=arm1176jzf-s  -mfpu=vfp
If you have some 64-bit integers this may (or may not) help:-

Code: Select all

-mneon-for-64bits
Some basic options that should be the defaults anyway:-

Code: Select all

-mfloat-abi=hard -mlittle-endian -munaligned-access
I think the crypto extensions are for 64-bit mode only?

Perhaps we should have a sticky for this sort of thing and some stuff to help beginners compile and run a hello world program.

MikeMelbourne
Posts: 5
Joined: Mon Apr 11, 2016 10:55 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Wed Apr 13, 2016 9:48 pm

jahboater wrote:

Code: Select all

-mneon-for-64bits
According to the gcc docs, this enables using Neon to handle scalar 64-bits operations. This is disabled by default since the cost of moving data from core registers to Neon is high.
jahboater wrote:I think the crypto extensions are for 64-bit mode only?
I didn't know this. I'm happy to keep away from it as my primary goal is gaming performance, which hopefully uses little to no cryptographic maths :)

lemassykoi
Posts: 2
Joined: Fri Apr 14, 2017 9:51 am

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Mon Apr 17, 2017 2:10 am

thank you so much !! your flags allowed me to compile pulseaudio 7.1 and 10 without errors on raspberry pi 3 (without flags, there was a lot of warnings)

raspberry.pie
Posts: 1
Joined: Sun Sep 17, 2017 5:04 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Sun Sep 17, 2017 5:13 pm

MikeMelbourne wrote:
Wed Apr 13, 2016 5:33 am
OK, I've come up with the following:
...
...
...
Hopefully I'll get some time shortly to try it out. Thanks for your input guys. :)
Hi, thank you for the post, I'm using crc and crypto flags (see below) and was wondering if they are better. I found them via https://www.cryptopp.com/wiki/ARM_(Command_Line) and the gcc docs. Thanks you again.

Code: Select all

-march=armv8-a+crc -mfpu=crypto-neon-fp-armv8

cga
Posts: 4
Joined: Sat Jun 24, 2017 4:13 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Mon Jun 25, 2018 6:48 pm

I now want to ask the same question about the 3B+ running
Linux rpi 4.14.50-v7+ #1122 SMP Tue Jun 19 12:26:26 BST 2018 armv7l GNU/Linux
and a question as to why the suggested ways to answer this question don't seem to work.
Q1: What are the best compiler flags for the 3B+ for compiling stuff like OpenCV 3.4.1?
Q2: What is the recommended way to figure this out by querying the machine?
Q3: Why do the recommended ways below not work?
Thanks,
Chris

gcc -mcpu=native -march=native -Q --help=target
says
-march= armv6
-mfpu= vfp
I know these are not right.

cat /proc/cpuinfo
says
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
...
This doesn't look right either.

I have been using the following for the 3B:
CFLAGS="-march=armv8-a+crc -mfpu=neon-vfpv4 -mtune=cortex-a53 -ftree-vectorize -mfloat-abi=hard -O3"

jahboater
Posts: 4681
Joined: Wed Feb 04, 2015 6:38 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Mon Jun 25, 2018 7:50 pm

cga wrote:
Mon Jun 25, 2018 6:48 pm
I have been using the following for the 3B:
CFLAGS="-march=armv8-a+crc -mfpu=neon-vfpv4 -mtune=cortex-a53 -ftree-vectorize -mfloat-abi=hard -O3"
That should work for the 3B+ too, its the same processor.

I simply use:-

-march=native

cga
Posts: 4
Joined: Sat Jun 24, 2017 4:13 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Tue Jun 26, 2018 1:21 pm

Presumably if
gcc -mcpu=native -march=native -Q --help=target
gives the wrong answer (armv6), then using -march=native gives the wrong answer when used to actually compile code. Querying the processor type using gcc used to work.

LdB
Posts: 1233
Joined: Wed Dec 07, 2016 2:29 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Tue Jun 26, 2018 4:25 pm

I truely doubt many GCC compiler providers seriously do very much with optimizing the native flags because probably the vast number of people working on GCC code base are cross compiling from a desktop version of linux, mac or windows. The native processor report would be AMD or Intel processor which has nothing to do with the target and hence you are forced to specify it. I know that some users are compiling on the Pi but it takes to long to compile the GCC sources on the Pi for it to be really practical to do GCC development on it. No GCC developers means little interest in getting things perfect for things like the native settings.

jahboater
Posts: 4681
Joined: Wed Feb 04, 2015 6:38 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Thu Jun 28, 2018 5:20 pm

LdB wrote:
Tue Jun 26, 2018 4:25 pm
means little interest in getting things perfect for things like the native settings.
Its true it took longer for "-march=native" to work properly on ARM than it did for x86.

Nowadays though, -march=native produces an identical executable on all Pi's (with recent versions of GCC anyway), so I have stopped giving the options.

jahboater
Posts: 4681
Joined: Wed Feb 04, 2015 6:38 pm

Re: Best Compilation Flags for RPi3 on Raspbian 32 bit?

Thu Jun 28, 2018 7:13 pm

cga wrote:
Tue Jun 26, 2018 1:21 pm
Presumably if
gcc -mcpu=native -march=native -Q --help=target
gives the wrong answer (armv6), then using -march=native gives the wrong answer when used to actually compile code. Querying the processor type using gcc used to work.
Still does I think.
This is the output on a Pi3+ ....

Code: Select all

[email protected]:~ $ gcc -mcpu=native -march=native -Q --help=target
The following options are target specific:
  -mabi=                      		aapcs-linux
  -mabort-on-noreturn         		[disabled]
  -mandroid                   		[disabled]
  -mapcs                      		[disabled]
  -mapcs-frame                		[disabled]
  -mapcs-reentrant            		[disabled]
  -mapcs-stack-check          		[disabled]
  -march=                     		armv8-a+crc+simd
  -marm                       		[enabled]
  -masm-syntax-unified        		[disabled]
  -mbe32                      		[enabled]
  -mbe8                       		[disabled]
  -mbig-endian                		[disabled]
  -mbionic                    		[disabled]
  -mbranch-cost=              		-1
  -mcallee-super-interworking 		[disabled]
  -mcaller-super-interworking 		[disabled]
  -mcmse                      		[disabled]
  -mcpu=                      		cortex-a53
  -mfix-cortex-m3-ldrd        		[disabled]
  -mflip-thumb                		[disabled]
  -mfloat-abi=                		hard
  -mfp16-format=              		none
  -mfpu=                      		neon-fp-armv8
  -mglibc                     		[enabled]
  -mhard-float                		
  -mlittle-endian             		[enabled]
  -mlong-calls                		[disabled]
  -mmusl                      		[disabled]
  -mneon-for-64bits           		[disabled]
  -mpic-data-is-text-relative 		[enabled]
  -mpic-register=             		
  -mpoke-function-name        		[disabled]
  -mprint-tune-info           		[disabled]
  -mpure-code                 		[disabled]
  -mrestrict-it               		[disabled]
  -msched-prolog              		[enabled]
  -msingle-pic-base           		[disabled]
  -mslow-flash-data           		[disabled]
  -msoft-float                		
  -mstructure-size-boundary=  		8
  -mthumb                     		[disabled]
  -mthumb-interwork           		[disabled]
  -mtls-dialect=              		gnu
  -mtp=                       		cp15
  -mtpcs-frame                		[disabled]
  -mtpcs-leaf-frame           		[disabled]
  -mtune=                     		
  -muclibc                    		[disabled]
  -munaligned-access          		[enabled]
  -mvectorize-with-neon-double 		[disabled]
  -mvectorize-with-neon-quad  		[enabled]
  -mword-relocations          		[disabled]

  Known ARM ABIs (for use with the -mabi= option):
    aapcs aapcs-linux apcs-gnu atpcs iwmmxt

  Known __fp16 formats (for use with the -mfp16-format= option):
    alternative ieee none

  Known ARM FPUs (for use with the -mfpu= option):
    auto crypto-neon-fp-armv8 fp-armv8 fpv4-sp-d16 fpv5-d16 fpv5-sp-d16 neon neon-fp-armv8 neon-fp16
    neon-vfpv3 neon-vfpv4 vfp vfp3 vfpv2 vfpv3 vfpv3-d16 vfpv3-d16-fp16 vfpv3-fp16 vfpv3xd
    vfpv3xd-fp16 vfpv4 vfpv4-d16

  Valid arguments to -mtp=:
    auto cp15 soft

  Known floating-point ABIs (for use with the -mfloat-abi= option):
    hard soft softfp

 TLS dialect to use:
    gnu gnu2

Return to “C/C++”