• AVR Freaks

Locked[FAQ]-G compiler option

Author
Deenayd
Super Member
  • Total Posts : 905
  • Reward points : 0
  • Joined: 2004/09/08 06:15:13
  • Location: Poland
  • Status: offline
2008/12/02 00:07:30 (permalink)
5 (1)

-G compiler option

I suggest adding a large -G compiler option as a defaulf on processor that has not more than 64k of RAM (currently on all PIC32's). For example using compiler option -G9000 causes all static data structures smaller than 9000 bytes to be placed in small data sections instead of standard ones. The effect is those variables are addressible using GP and this is much faster than ordinary lui + ori + access sequence.
 
Up to 64k bytes of RAM can be addressed using GP so processors with up to 64k RAM can address all static variables using GP.
 
Currently we can use this option as a parameter in "alternate settings" so anybody can check what is the result of making this change for his/her programs.

Slawek Piotrowski
Rejestracja Czasu Pracy Ewidencja Czasu Pracy
#1

11 Replies Related Threads

    Kiwi Mike AZ
    Super Member
    • Total Posts : 2044
    • Reward points : 0
    • Status: offline
    RE: -G compiler option 2008/12/02 00:18:17 (permalink)
    0
    Just tested it with a current project.

    Reduced the code size from 13713 words down to 13471 words.... 242 instruction saving
    Guess a speed advantage as well... especially since it uses less instructions to do the same thing.

    Thanks for the useful tip.

    Mike

    EDIT: The above was without any optimisation
    adding -Os
     8228 words without -G
     8132 words with -G9000
     ------------------------
      96 word/instruction saving



    post edited by mimemike - 2008/12/02 00:22:50
    #2
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 08:52:40 (permalink)
    0
    Thanks for the suggestion. I'll discuss it with the team.

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #3
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: -G compiler option 2008/12/02 10:58:30 (permalink)
    0
     suggest adding a large -G compiler option as a defaulf on processor that has not more than 64k of RAM

     
    I did some experimenting and I think the situation is a little more subtle:
     
    First I inspected my code and found that a lot of variables are already addressed relative to gp by default. So I decided to add -G0 and indeed, code size became a lot larger (around 10%). So it seems that there is already a -G in effect with an undocumented default value.
     
    Also the C32 user manual explicitly states "All modules should be compiled with the same -G num value" which makes me wonder what happens if for instance a library is used whose modules are compiled using a different -G value than the other sources in the application. The user manual says nothing about this.
     
    My suggestion would be to explicitly place variables you want to address relative to gp with
     
    __attribute__ ((section(".sbss")))
     
     
    Also the following, which I reported to this forum before, seems interesting. My application uses a number of global 4byte sized variables which are explicitly placed in the .sbss section.
     
    Some assembly module is using those variables but the variables are external to the assembly module. The assembly module contains:
     
    .extern variable
     
    and even though the variables are in .sbss, the assembly module does not access them relative to gp. When I however write:
     
    .extern variable SZPTR
     
    The variables are accessed relative to gp. It seems that by telling the assembler/linker the external variables are 4 bytes in size they are addressed relative to gp again leading to the assumption there is some default value in effect for -G.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #4
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 11:07:21 (permalink)
    0
    The default value is 8 bytes, which is large enough to hold all simple scalar variables. I'll make a note to add the default value to the user's guide.


    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #5
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: -G compiler option 2008/12/02 11:12:04 (permalink)
    0
    Thanks Jason,
     
    so I understand the following:
     
    All variables <= 8bytes are automatically placed in .sbss and addressed relative to gp
     
    Variables > 8 bytes are not, but by adding __attribute__ ((section(".sbss"))) you still can force them to be addressed relative to gp
     
    Is this correct?


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #6
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 11:25:04 (permalink)
    0
    Variables > 8 bytes are not, but by adding __attribute__ ((section(".sbss"))) you still can force them to be addressed relative to gp

    That's right. Also, as you've already stated above, you must take special care when writing assembler code to declare global data items correctly:
    • Writable, initialised data of gnum bytes or less must be put explicitly into the .sdata section, e.g.:
      .sdata
      small: .word 0x12345678
    • Global common data must be declared with the correct size, e.g:
      .comm small, 4
      .comm big, 100
    • Small external variables must also be declared correctly, e.g:
      .extern smallext, 4
    Also, as Deenayd pointed out, you can use larger values of −Gnum to increase the scope of this optimization. However, at the moment the only way to find the limit is an iterative process of recompiling with increasing values, until you overflow the 64K limit. When you overflow the limit, you'll get a linker relocation error related to GP-relative addressing. One day the compiler might adjust the default value based on the selected device's available memory. Alternatively, we might be able to teach the toolchain to determine an optimal default value for the project.

    Thanks everyone for the suggestions.

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #7
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 11:38:23 (permalink)
    0
    I should have also mentioned that all of our prebuilt libraries are built with -G0, which disables gp-relative addressing. This value prevents conflicts with other modules.

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #8
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: -G compiler option 2008/12/02 11:44:58 (permalink)
    0
    Ok Jason,
     
    One more question: What happens when mixing libraries built with different values for -G?


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #9
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 13:42:45 (permalink)
    0
    One more question: What happens when mixing libraries built with different values for -G?

    You could end up with a conflict if the compiler assumes that a variable is located in a small section while it actually resides in a non-small section. In this case, you'd get a relocation error from the linker.

    To avoid this, we build our libraries with -G0. We then place non-small variables, such as SFR variables, in a non-small section (e.g. __attribute__((section("sfrs")))).

    For the few global variables in the libraries, such as errno, we use __attribute__((section(".sbss"))) to place them in the small bss section. They will then be accessed with gp-relative addressing only when the user's gnum value is appropriate.

    Hope this helps!

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #10
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: -G compiler option 2008/12/02 14:43:11 (permalink)
    0
    So if I get it right, when I place all my global data in small sections using __attribute__ ((section("sbss"))) and build a library using a value for -G where all my global var's fit in, there will never be a problem.
     
    What I understand is when the user links my library and uses -G0, the variables will not be accessed through gp but there will not be a linker error.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #11
    JasonK
    Moderator
    • Total Posts : 3383
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    RE: -G compiler option 2008/12/02 14:50:56 (permalink)
    0
    What I understand is when the user links my library and uses -G0, the variables will not be accessed through gp but there will not be a linker error.

    Right.

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5