User avatar
HermannSW
Posts: 4131
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

godbolt compiler explorer

Sun May 30, 2021 9:40 pm

TIL from my older son on godbolt.org compiler explorer.
I tried it for Intel x86-64 gcc on a small sample.
Cool how color coding of C lines and compiled assembler visualize what belongs to what.
And even cooler how the whole assembler code collapses after adding "-O" compiler option:
Image


Works for many languages (I am mostly interested in C).
And under "platform specific compilers" there is "Raspbian Buster" as well !
godbolt.buster.png
godbolt.buster.png
godbolt.buster.png (123.34 KiB) Viewed 540 times
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/raspiraw
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: godbolt compiler explorer

Mon May 31, 2021 12:54 am

Godbolt is a wonderful thing isn't it.

Amazing how many language, compilers and architectures it supports now.
Memory in C++ is a leaky abstraction .

User avatar
stephj
Posts: 82
Joined: Thu Jun 21, 2012 1:20 pm
Location: Lancashire, UK

Re: godbolt compiler explorer

Wed Jun 09, 2021 9:01 pm

It is interesting to observe how it deals with 'C' integer division and % (MOD) operators without actually using DIV instructions, which are not available on all ARM implementations.

Bump up the optimization as high as it will go, then study the ARM code it generates. Clever use of 'magic' numbers, multiply then left shifts and multiply-subtract MLS instructions all do the job in 5 or 6 machine instructions. Neat!''

User avatar
HermannSW
Posts: 4131
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: godbolt compiler explorer

Thu Jun 10, 2021 1:41 pm

stephj wrote:
Wed Jun 09, 2021 9:01 pm
It is interesting to observe how it deals with 'C' integer division and % (MOD) operators without actually using DIV instructions, which are not available on all ARM implementations.

Bump up the optimization as high as it will go, then study the ARM code it generates. Clever use of 'magic' numbers, multiply then left shifts and multiply-subtract MLS instructions all do the job in 5 or 6 machine instructions. Neat!''
Can you provide a share link showing that?

I tried to see what you reported and computed long division as well as long mod:

Code: Select all

long a=1<<30;
long b=2<<15-1;
long d = a/b;
long r = a%b;
I selected "Raspberry Buster" as compiler and used "-O3", here is share link:
https://godbolt.org/z/dhfnYqono

The assemler code shows "bl __aeabi_idivmod".
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/raspiraw
https://stamm-wilbrandt.de/en/Raspberry_camera.html

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 28680
Joined: Sat Jul 30, 2011 7:41 pm

Re: godbolt compiler explorer

Thu Jun 10, 2021 3:54 pm

I've never heard of godbolt before, but that's a fantastic web resource!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Application's Team.

User avatar
HermannSW
Posts: 4131
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: godbolt compiler explorer

Thu Jun 10, 2021 4:22 pm

jamesh wrote:
Thu Jun 10, 2021 3:54 pm
I've never heard of godbolt before, but that's a fantastic web resource!
It supports "Buster" and "Stretch" compilers for Raspberry.
Perhaps you might want to get in contact with them to provide "Raspberry Pi OS 64bit" as option as well ...
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/raspiraw
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: godbolt compiler explorer

Thu Jun 10, 2021 7:13 pm

jamesh wrote:
Thu Jun 10, 2021 3:54 pm
I've never heard of godbolt before, but that's a fantastic web resource!
It is, isn't it?

I'm amazed you don't get out much and come across it before. It's been a celebrity in the C++ world for some time now where "Godbolt it" is a thing as much as "Google it".

I'm further amazed it has grown to encompass other languages as well.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 7077
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: godbolt compiler explorer

Thu Jun 10, 2021 7:43 pm

HermannSW wrote:
Thu Jun 10, 2021 4:22 pm
Perhaps you might want to get in contact with them to provide "Raspberry Pi OS 64bit" as option as well ...
It already supports Clang and GCC compilations for Aarch64.

GCC is supported from version 5.4 to the latest which is 11.1
Clang is supported from 9 to 11 and trunc.

The OS release seems irrelevant to me.
However, I think the default compiler on Raspberry Pi OS 64bit is GCC 8.3.
Godbolt supports 8.2 and 9.3 which is near enough I would say.
I usually find the latest compiler versions most interesting.

User avatar
jahboater
Posts: 7077
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: godbolt compiler explorer

Thu Jun 10, 2021 9:56 pm

See the original brilliant presentation:
https://www.youtube.com/watch?v=bSkpMdDe4g4
and more recently, even better “What Else Has My Compiler Done For Me Lately?” ...
https://www.youtube.com/watch?v=nAbCKa0FzjQ

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

Re: godbolt compiler explorer

Fri Jun 11, 2021 1:13 am

jahboater wrote:
Thu Jun 10, 2021 7:43 pm
HermannSW wrote:
Thu Jun 10, 2021 4:22 pm
Perhaps you might want to get in contact with them to provide "Raspberry Pi OS 64bit" as option as well ...
It already supports Clang and GCC compilations for Aarch64.

GCC is supported from version 5.4 to the latest which is 11.1
Clang is supported from 9 to 11 and trunc.

The OS release seems irrelevant to me.
However, I think the default compiler on Raspberry Pi OS 64bit is GCC 8.3.
Godbolt supports 8.2 and 9.3 which is near enough I would say.
I usually find the latest compiler versions most interesting.
For some x86 systems and programs I've found the GCC 4.x compiler generates faster executables than any of the more recent compilers. Since the 2.x compilers were contemporary with the creation of Linux, it would be nice if the website went back that far.

Along similar lines there is a notable performance regression for 32-bit ARM on Raspberry Pi starting 7.x. Unlike the x86 regression, that one was fixed in 10.x.

I've never tried to track down what caused the changes; however, my impression is instruction sequencing is much more important with in-order cores but that these days most regression tests are being run on out-of-order cores.

For the next step in compiler optimization I think it will be important to take into account the relative performance of memory, cache and CPU because this can vary significantly between different configurations based on the same processor, for example, not all Cortex-A72 based systems run at the same clock speed or use the same kind of RAM as the Raspberry Pi 4B.

User avatar
stephj
Posts: 82
Joined: Thu Jun 21, 2012 1:20 pm
Location: Lancashire, UK

Re: godbolt compiler explorer

Fri Jun 11, 2021 3:08 pm

HermannSW Wrote: Can you provide a share link showing that?
Sorry what I failed to mention that I was requiring MOD and DIV by a specific value, that being 10.

The original problem was to write an ARM code program to calculate then print out factorial 12 (12!). Calculating 12! is a hoot, but printing it out in a decimal form in ARM assembly is not that easy. I used functions MOD10( int i) and DIV10(int i) where the respective function code was:

return(i%10); and return(i/10); The '10' ends up as static number which allows the optimizer to go to town with it. I then lifted the compiler code and adapted them as a subroutines in my own code. Other parts of the code were lifted directly from ARM Linux "Hello World"

The Code follows, its not 'pretty' but it works. The assembler gripes about the umull instructions using the same registers but that is an older ARM restriction.

The use of the magic numbers etc. can be seen at the bottom of the code in the MOD10 and DIV10 subroutines.

Code: Select all

.data
F12OUT:
    .ascii      "xxxxxxxxx\n"
    len = . - F12OUT
B2A:
    .ascii     "0123456789"
F12DIV: .long 0
F12MOD: .long 0

.text
.globl _start
_start:

    mov     r0, #12
    mov     r1, #1

FLoop:                      @ Calculate 12!
    mul		r1,r0,r1
    subs	r0,r0,#1
    bne	    FLoop
    
    ldr     r0, =F12DIV     @ Store 12! in F12DIV
    str     r1,[r0]
    ldr     r4,=B2A
    ldr     r5,=F12OUT
    add     r5,r5,#8        @ Point r5 to last x in F12OUT

DLoop:
    ldr     r1, =F12DIV
    ldr     r0,[r1]
    bl      MOD10           @ Find value of F12DIV MOD 10
    ldr     r1, =F12MOD
    str     r0,[r1]
    ldrb    r6,[r4,r0]      @ Convert to char in B2A
    strb    r6,[r5]         @ Store in F12OUT
    sub     r5,r5,#1        @ Decrement r5
    ldr     r1, =F12DIV
    ldr     r0,[r1]
    bl      DIV10           @ Find value of F12DIV DIV 10
    ldr     r1, =F12DIV     @ Save back to F12DIV
    str     r0,[r1]
    cmp     r0,#0
    bne     DLoop           @ Repeat until F12DIV is Zero

    /* syscall write(int fd, const void *buf, size_t count) */
    mov     r0, #1      /* fd -> stdout */
    ldr     r1, =F12OUT /* buf ->  */
    ldr     r2, =len    /* count -> len(msg) */
    mov     r7, #4      /* write is syscall #4 */
    swi     0           /* invoke syscall */

    /* syscall exit(int status) */
    eor     r0, r0      /* status -> 0 */
    mov     r7, #1      /* exit is syscall #1 */
    swi     0           /* invoke syscall */

MOD10:
    movw    r3, #52429
    movt    r3, 52428
    movs    r2, #10 
    umull   r1, r3, r3, r0
    lsrs    r3, r3, #3   
    mls     r0, r2, r3, r0   
    bx      lr 
DIV10:
    movw    r3, #52429
    movt    r3, 52428
    umull   r3, r0, r3, r0
    lsrs    r0, r0, #3
    bx      lr
To prove it works:

Code: Select all

479001600
------------------
(program exited with code: 0)
Press return to continue

Return to “C/C++”