• AVR Freaks

Hot!optimization level 1 questions

Author
MPaulHolmes
Junior Member
  • Total Posts : 83
  • Reward points : 0
  • Joined: 2009/10/31 10:52:40
  • Location: 0
  • Status: offline
2020/02/07 13:45:28 (permalink)
0

optimization level 1 questions

I have code that needs to execute in a certain order.  I already know that the compiler just gets rid of stuff that it finds repetitive. Does it also change the order of execution if it thinks that's a good idea?  There's an "enable top level reordering".  That doesn't mean that they actually change the order of code execution, do they?
#1

10 Replies Related Threads

    LostInSpace
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2016/03/11 22:47:59
    • Location: 0
    • Status: offline
    Re: optimization level 1 questions 2020/02/09 08:58:28 (permalink)
    0
    Near as I can figure, XC32 does indeed reorder 'stuff', mostly math that does not matter as to order to improve execution speed or size, but I have never seen it reorder output statements for example because that would be disastrous (naturally). Generally these sorts of things are well thought out and never cause anyone any issues.
     
    From the GCC manual you can find this switch to turn it off,
     
    -fno-toplevel-reorder    Do not reorder top-level functions, variables, and asm statements. Output them in the same order that they appear in the input file. When this option is used, unreferenced static variables are not removed. This option is intended to support existing code that relies on a particular ordering. For new code, it is better to use attributes when possible.    -ftoplevel-reorder is the default at -O1 and higher, and also at -O0 if -fsection-anchors is explicitly requested. Additionally -fno-toplevel-reorder implies -fno-section-anchors.
    https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
     
    Also the compiler options that GCC applies does not necessarily apply to XC32 - I compiled the 'full list' by geting the compiler (XC32, 2.05) to 'tell all' here,
    https://analoghome.blogspot.com/2019/08/microchip-xc32-compiler-optimization.html
     
    Hope this helps
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 18673
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: optimization level 1 questions 2020/02/09 09:16:47 (permalink)
    +1 (1)
    Per the C standard the compiler can not change the order of the code execution if it affect the way the code will work.
    So it should work the way you expect.
    If you are doing something exceptionally creative, it may not.

    LostInSpace. That is how functions and variables are ordered in memory. Not execution.
    #3
    LostInSpace
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2016/03/11 22:47:59
    • Location: 0
    • Status: offline
    Re: optimization level 1 questions 2020/02/11 20:14:56 (permalink)
    0
    RE: toplevel-reorder
    >>>That is how functions and variables are ordered in memory. Not execution.
     
    Well you are the expert, but according to this it is different execution flow,
    https://github.com/gcc-mirror/gcc/blob/master/gcc/bb-reorder.c

     
    Another interesting discussion,
    https://blog.regehr.org/archives/232

     
    But the bottom line (as you say) is - these optimizations don't seem to cause any issues for anyone unless they are doing something really esoteric - or doing the optimization themselves, which the compiler then misinterprets.
    post edited by LostInSpace - 2020/02/11 20:20:25
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11824
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: optimization level 1 questions 2020/02/11 21:49:57 (permalink)
    +1 (1)
    I'm pretty sure -fno-toplevel-reorder has nothing to do with instruction reordering optimizations that the OP is asking about.  It controls the way code is emitted into object files.
    #5
    LostInSpace
    Super Member
    • Total Posts : 306
    • Reward points : 0
    • Joined: 2016/03/11 22:47:59
    • Location: 0
    • Status: offline
    Re: optimization level 1 questions 2020/02/12 09:18:59 (permalink)
    0
    OK, I agree - Thanks for helping me to see this. 
     
    Top Level Reorder: is the re-arranging of functions, variables and asm statements in memory. If you were expecting some specific memory variable, function or asm statement placement and just plucking them from memory yourself, this optimization (top level reorder) will probably break what you are doing.
     
    "Our internal testing showed that a few customer applications relied on some variables being allocated in the same order that they appear in the source code, 
    causing customer applications to "break". We decided to disable this option by default so we had some time to educate customers that they should not rely
    on ordering without using an attribute to enforce it. This has the side effect of causing unused static variables to not be removed.
    We plan to re-enable this option by default at some point in the future."
    https://github.com/chipKIT32/chipKIT32-MAX/issues/617
    #6
    Mysil
    Super Member
    • Total Posts : 3670
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: optimization level 1 questions 2020/02/12 11:43:56 (permalink)
    0
    Hi,
    As far as I understand:
    Design principles behind Reduced Instruction Set Computer (RISC) ,
    and corresponding Compiler support,
    is based on the Compiler should Optimize code by keeping variables in CPU registers,
    Read variables from Memory before they are asked for, and defer writing to memory until convinient.
    Also, Compiler is expected to reorder instructions from different statements, when results will be the same.
     
    There are exceptions however:
    When Memory or Peripheral register values may change because of reasons that the Compiler cannot know about,
    that is for reason that is Not specified in the current Compilation Unit.
    Compilation unit is a single .c source file and any .h header files that are #included.
     
    Then C language have the possibility to specify variables and peripheral SFR registers with keyword:
    volatile  
    This is a directive to the compiler, that optimizations must be restricted, such that read and write operations are performed as programmed. 
    When reading or writing the same variable repeatedly, they must be repeated,
    and sequence of operations cannot be changed.
     
    In  device support files distributed together with XC32 compiler,
    #include <xc.h>
    all peripheral SFR registers are specified with keyword volatile.
     
    So operations using symbolic register names from the device support files, shall be safe from beeing reordered or skipped by the compiler.
     
    When using XC32 compiler, it is usually only when defining variables for communicating between interrupt code and mainline code, and variables for communication between interrupt code with different priority level,
    that programmer need to care about volatile specifier in declarations.
     
    However, volatile specification do not solve all possible problems,
    what will happen if code test a variable or register, and it have changed when using the value in the next statement?
    Also, it will make code slower if used when not needed.
     
        Mysil
    #7
    JasonK
    Moderator
    • Total Posts : 3406
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    Re: optimization level 1 questions 2020/02/21 11:18:08 (permalink)
    +2 (2)
    The C compiler will always respect C sequence points as defined by the language standard regardless of the optimization level.
     

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #8
    Mysil
    Super Member
    • Total Posts : 3670
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: optimization level 1 questions 2020/02/21 13:38:40 (permalink)
    +1 (1)
    Hi,
    And what is the effect of a sequence point?
    Language standard talk about evaluation of expressions and side effects of expressions.
     
    For a Load and Store CPU with 32 GPR Registers,
    What is the relation between sequence point and Memory hierarchy?
        CPU registers     FPU registers    CP0 registers
                        Cache
        SFR registers   RAM Memory 

    Is a value computed and stored in one of the 32 CPU core registers valid for a sequence point?
    Is a value stored in a FPU register valid for a sequence point?
    What difference does a Variable or SFR register declared 'volatile' make?
     
        Mysil
    #9
    JasonK
    Moderator
    • Total Posts : 3406
    • Reward points : 0
    • Joined: 2003/11/14 09:49:40
    • Location: Microchip Technology in Arizona, USA
    • Status: offline
    Re: optimization level 1 questions 2020/02/21 14:59:46 (permalink)
    +4 (4)
    Sequence points and the storage location aren't directly related. The logical value may be in a register, cache, SRAM, etc.
     
    For the volatile qualifier:
    "The standard encourages compilers to refrain from optimizations concerning accesses to volatile objects but leaves it implementation defined as to what constitutes a volatile access. The minimum requirement is that at a sequence point, all previous accesses to volatile objects have stabilized and no subsequent accesses have occurred. Thus an implementation is free to reorder and combine volatile accesses that occur between sequence points, but cannot do so for accesses across a sequence point. The use of volatile does not allow you to violate the restriction on updating objects multiple times between two sequence points." 
    https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html
     

    Jason Kajita
     Follow me on Twitter
    http://support.microchip.com for urgent questions
    #10
    NorthGuy
    Super Member
    • Total Posts : 6099
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: optimization level 1 questions 2020/02/23 12:11:35 (permalink)
    +1 (1)
    As long as the sequence points are obeyed, the compiler can rearrange code freely. For example:
     
    int x = 0;
    int foo() {
      return x++;
    }
    int bar() {
      return x--;
    }
     
    ...
     
    z = foo() + bar();

     
    The compiler may chose to execute foo() first (making z = 1), or bar() first (making z = -1). Of course, in such situation, the sequence may be different at different optimization levels.
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5