Compiler optimizations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sammy

    Compiler optimizations

    Word up!

    If there are any gcc users here, maybe you could help me out. I have a
    program and I've tried compiling it with -O2 and -O3 optimization
    settings.

    The wired thing is that it actually runs faster with -O2 than with -O3,
    even though -O3 is a higher optimization setting.

    Have I found a bug in gcc? Could I be doing something wrong?

    Cheers.

  • jameskuyper@verizon.net

    #2
    Re: Compiler optimizations

    sammy wrote:
    ....
    If there are any gcc users here, maybe you could help me out. I have a
    program and I've tried compiling it with -O2 and -O3 optimization
    settings.
    >
    The wired thing is that it actually runs faster with -O2 than with -O3,
    even though -O3 is a higher optimization setting.
    >
    Have I found a bug in gcc? Could I be doing something wrong?
    The answer to both questions is "possibly". WIthout knowing the
    details of your code AND the details of gcc's optimization strategies,
    we can't be sure. Something that is in general an optimization could
    easily, for a specific program using specific inputs, be a
    pessimization instead.

    For gcc issues, I'd recommend using a forum specialized for gcc; this
    isn't it.

    Comment

    • user923005

      #3
      Re: Compiler optimizations

      On Jan 15, 3:43 pm, sammy <s...@noemail.s pamwrote:
      Word up!
      >
      If there are any gcc users here, maybe you could help me out. I have a
      program and I've tried compiling it with -O2 and -O3 optimization
      settings.
      >
      The wired thing is that it actually runs faster with -O2 than with -O3,
      even though -O3 is a higher optimization setting.
      >
      Have I found a bug in gcc? Could I be doing something wrong?
      Higher optimization levels just means that compilers are requested to
      use more esoteric types of optimization tricks. There is no guarantee
      that {for instance} -O1 is faster than -O0 for that matter.
      Take the example of inlining... It may make the code run faster due to
      reduced function calls or it may make the code so large that stuff
      that used to fit in the cache no longer does.

      I suggest you direct specific performance problems with the GCC
      compiler to the GCC compiler newsgroups.

      P.S.
      You can get good results with recent versions of GCC by using profile
      guided optimization.

      Comment

      • CBFalconer

        #4
        Re: Compiler optimizations

        jameskuyper@ver izon.net wrote:
        sammy wrote:
        ...
        >If there are any gcc users here, maybe you could help me out. I have a
        >program and I've tried compiling it with -O2 and -O3 optimization
        >settings.
        >>
        >The wired thing is that it actually runs faster with -O2 than with -O3,
        >even though -O3 is a higher optimization setting.
        >>
        >Have I found a bug in gcc? Could I be doing something wrong?
        >
        The answer to both questions is "possibly". WIthout knowing the
        details of your code AND the details of gcc's optimization strategies,
        we can't be sure. Something that is in general an optimization could
        easily, for a specific program using specific inputs, be a
        pessimization instead.
        >
        For gcc issues, I'd recommend using a forum specialized for gcc; this
        isn't it.
        And he may be getting confused by the quantum effects on the system
        timer.

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.



        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Kelsey Bjarnason

          #5
          Re: Compiler optimizations

          On Wed, 16 Jan 2008 00:43:25 +0100, sammy wrote:
          Word up!
          >
          If there are any gcc users here, maybe you could help me out. I have a
          program and I've tried compiling it with -O2 and -O3 optimization
          settings.
          >
          The wired thing is that it actually runs faster with -O2 than with -O3,
          even though -O3 is a higher optimization setting.
          >
          Have I found a bug in gcc? Could I be doing something wrong?
          One point to ponder, which isn't specific to gcc but to optimisation in
          general...

          Many optimisations consume more space than the less optimised code. Loop
          unrolling, for example, can do this. While this _can_ result in faster
          code, it can _also_ potentially result in side effects such as exhausting
          the cache memory. The net result can be a significant slowdown.

          This sort of thing isn't really a bug; the optimiser has no way to know
          what machines the code will run on.

          Comment

          • Joachim Schmitz

            #6
            Re: Compiler optimizations

            Kelsey Bjarnason wrote:
            On Wed, 16 Jan 2008 00:43:25 +0100, sammy wrote:
            >
            >Word up!
            >>
            >If there are any gcc users here, maybe you could help me out. I have
            >a program and I've tried compiling it with -O2 and -O3 optimization
            >settings.
            >>
            >The wired thing is that it actually runs faster with -O2 than with
            >-O3, even though -O3 is a higher optimization setting.
            >>
            >Have I found a bug in gcc? Could I be doing something wrong?
            >
            One point to ponder, which isn't specific to gcc but to optimisation
            in general...
            >
            Many optimisations consume more space than the less optimised code.
            Loop unrolling, for example, can do this. While this _can_ result in
            faster code, it can _also_ potentially result in side effects such as
            exhausting the cache memory. The net result can be a significant
            slowdown.
            >
            This sort of thing isn't really a bug; the optimiser has no way to
            know what machines the code will run on.
            If not the compiler/optimizer, who else?

            Bye, Jojo


            Comment

            • Malcolm McLean

              #7
              Re: Compiler optimizations

              "Kelsey Bjarnason" <kbjarnason@gma il.comwrote in message
              >
              This sort of thing isn't really a bug; the optimiser has no way to know
              what machines the code will run on.
              >
              What input is probably more significant.
              The length of an unbounded string is almost certainly a few tens of bytes or
              less, for instance, so it makes sense to run a byte-by-byte strlen().
              However if the string happens to be a DNA sequence then it may be hundreds
              of kilobytes long, and so aligning to a word boundary and doing 32 or 64 bit
              fetches will speed up code considerably. It is extremely difficult to tell a
              compiler the difference between a sequence and a username, they are both
              just strings of arbitrary length, to it.

              --
              Free games and programming goodies.


              Comment

              • Rui Maciel

                #8
                Re: Compiler optimizations

                Kelsey Bjarnason wrote:
                This sort of thing isn't really a bug; the optimiser has no way to know
                what machines the code will run on.
                If the compiler writers think that this is relevant and they need to figure
                out a way to know any property of the target machine, couldn't they simply
                add an option that enables the user to specify those target properties?


                Rui Maciel

                Comment

                • jacob navia

                  #9
                  Re: Compiler optimizations

                  sammy wrote:
                  Word up!
                  >
                  If there are any gcc users here, maybe you could help me out. I have a
                  program and I've tried compiling it with -O2 and -O3 optimization
                  settings.
                  >
                  The wired thing is that it actually runs faster with -O2 than with -O3,
                  even though -O3 is a higher optimization setting.
                  >
                  Have I found a bug in gcc? Could I be doing something wrong?
                  >
                  Cheers.
                  >
                  According to my experience with gcc all optimizations beyond 2 are
                  a waste of time.

                  The problem with gcc is that every person interested in compiler
                  algorithms has hacked gcc to put his/her contribution, making
                  the whole quite messy.

                  Within lcc-win, I have targeted only ONE optimization strategy:

                  Code size.

                  There is nothing that runs faster than a deleted instruction. Lcc-win
                  features a very simple peephole optimizer, that is after a single
                  goal: delete redundant loads/stores, and in general to try to
                  reduce the code size as much as possible.

                  No other optimizations are done (besides the obvious ones done at
                  compile time like constant folding, division by constants, etc)

                  Gcc tries it all. I think there is no optimization that exists
                  somewhere in compiler books that hasn't been tried in gcc.
                  Code movement/aligning of the stack/global CSE/
                  aggressive inlining/ and a VERY long ETC!

                  The result is not really impressing. the compiler is very slow
                  and the program is not very fast:

                  A matrix multiplication program for instance: (time in seconds)

                  lcc-win -O 1.851
                  gcc -O2 1.690
                  gcc -O3 1.802
                  gcc -O9 1.766
                  MSVC -Ox 1.427

                  With -O3 gcc is as slow as lcc-win (what is obviously an excellent
                  result ) And the delta between gcc and lcc-win in the best case
                  for gcc is just... 3.1%

                  If you look at the compilation speed of lcc-win vs gcc (a factor
                  of 5 or more) and the size of the source code (11MB of C for gcc,
                  1MB of C for lcc-win) things look clearer.

                  What is worst for the optimizer compilers is that CPUs are now
                  so complex that optimizations that before were fine like inlining
                  have completely lost all their justification now that a processor
                  can wait up to 50 cycles doing nothing waiting that the RAM
                  gives it the information.

                  In this context optimizing for SIZE is a winning strategy. And
                  allows lcc-win to have almost the same speed as gcc with a FRACTION
                  of the effort.

                  Just my $0.02



                  --
                  jacob navia
                  jacob at jacob point remcomp point fr
                  logiciels/informatique

                  Comment

                  • Stephen Sprunk

                    #10
                    Re: Compiler optimizations

                    "sammy" <sam@noemail.sp amwrote in message
                    news:slrnfoqh8t .u2a.nospam@nos pam.invalid...
                    If there are any gcc users here, maybe you could help me out. I have a
                    program and I've tried compiling it with -O2 and -O3 optimization
                    settings.
                    >
                    The wired thing is that it actually runs faster with -O2 than with -O3,
                    even though -O3 is a higher optimization setting.
                    That sometimes happens. Some optimizations, particularly at GCC's higher
                    levels, are not guaranteed: they pay off most of the time, but sometimes
                    they hurt you. Also, many of the optimizations depend on the compiler
                    knowing the exact characteristics of the machine you'll run the code on; if
                    you tell GCC you have a i386 or a P4, but run the code on an Opteron, you
                    may get slower execution than if you told it you had an Opteron or used a
                    lower optimization level.

                    Depending on the code, using profile-guided optimization can provide a
                    significant performance boost as the compiler has more data on your specific
                    program (and your input data) versus static predictions that are tuned for
                    the "average" program.
                    Have I found a bug in gcc? Could I be doing something wrong?
                    If you flip a coin and guess the wrong result, is the coin buggy? No.

                    A compiler bug is when it doesn't properly translate a correct program.
                    Unless you're an expert, the most likely cause of improper results is that
                    your program isn't as correct as you think it is. Many advanced
                    optimizations cause odd results in C's undefined corners that compiling
                    simpler optimizations (or none at all) won't expose.

                    S

                    --
                    Stephen Sprunk "God does not play dice." --Albert Einstein
                    CCIE #3723 "God is an inveterate gambler, and He throws the
                    K5SSS dice at every possible opportunity." --Stephen Hawking

                    Comment

                    • Randy Howard

                      #11
                      Re: Compiler optimizations

                      On Wed, 16 Jan 2008 05:24:07 -0600, Joachim Schmitz wrote
                      (in article <fmkpgn$v36$1@o nline.de>):
                      Kelsey Bjarnason wrote:
                      >On Wed, 16 Jan 2008 00:43:25 +0100, sammy wrote:
                      >>
                      >>Word up!
                      >>>
                      >>If there are any gcc users here, maybe you could help me out. I have
                      >>a program and I've tried compiling it with -O2 and -O3 optimization
                      >>settings.
                      >>>
                      >>The wired thing is that it actually runs faster with -O2 than with
                      >>-O3, even though -O3 is a higher optimization setting.
                      >>>
                      >>Have I found a bug in gcc? Could I be doing something wrong?
                      >>
                      >One point to ponder, which isn't specific to gcc but to optimisation
                      >in general...
                      >>
                      >Many optimisations consume more space than the less optimised code.
                      >Loop unrolling, for example, can do this. While this _can_ result in
                      >faster code, it can _also_ potentially result in side effects such as
                      >exhausting the cache memory. The net result can be a significant
                      >slowdown.
                      >>
                      >This sort of thing isn't really a bug; the optimiser has no way to
                      >know what machines the code will run on.
                      If not the compiler/optimizer, who else?
                      How can it possibly know which computer(s) you will install and run it
                      on after it is compiled?

                      Not every program is something for you to play with for a bit in ~/src
                      then forget about. ;-)



                      --
                      Randy Howard (2reply remove FOOBAR)
                      "The power of accurate observation is called cynicism by those
                      who have not got it." - George Bernard Shaw





                      Comment

                      • Randy Howard

                        #12
                        Re: Compiler optimizations

                        On Wed, 16 Jan 2008 06:47:46 -0600, Rui Maciel wrote
                        (in article <478e10fe$0$176 82$a729d347@new s.telepac.pt>):
                        Kelsey Bjarnason wrote:
                        >
                        >This sort of thing isn't really a bug; the optimiser has no way to know
                        >what machines the code will run on.
                        >
                        If the compiler writers think that this is relevant and they need to figure
                        out a way to know any property of the target machine, couldn't they simply
                        add an option that enables the user to specify those target properties?
                        Some provide switches to optimize for different "families" of
                        processors. The problem is, it doesn't allow for any new hardware that
                        comes along after the compile is completed or after the compiler was
                        written. Also, it doesn't allow for a binary to be used on multiple
                        hardware platforms from the same build while enjoying this special
                        attention.

                        --
                        Randy Howard (2reply remove FOOBAR)
                        "The power of accurate observation is called cynicism by those
                        who have not got it." - George Bernard Shaw





                        Comment

                        • Randy Howard

                          #13
                          Re: Compiler optimizations

                          On Wed, 16 Jan 2008 09:26:23 -0600, jacob navia wrote
                          (in article <fml7n2$iov$1@a ioe.org>):
                          Within lcc-win, I have targeted only ONE optimization strategy:
                          >
                          Code size.
                          >
                          There is nothing that runs faster than a deleted instruction.
                          <shakes head>

                          #include "examples of loop unrolling improving performance"

                          I wonder if everyone using lcc-win today realize just how narrow your
                          view on optimization is?


                          --
                          Randy Howard (2reply remove FOOBAR)
                          "The power of accurate observation is called cynicism by those
                          who have not got it." - George Bernard Shaw





                          Comment

                          • jacob navia

                            #14
                            Re: Compiler optimizations

                            Randy Howard wrote:
                            On Wed, 16 Jan 2008 09:26:23 -0600, jacob navia wrote
                            (in article <fml7n2$iov$1@a ioe.org>):
                            >
                            >Within lcc-win, I have targeted only ONE optimization strategy:
                            >>
                            >Code size.
                            >>
                            >There is nothing that runs faster than a deleted instruction.
                            >
                            <shakes head>
                            >
                            #include "examples of loop unrolling improving performance"
                            >
                            I wonder if everyone using lcc-win today realize just how narrow your
                            view on optimization is?
                            >
                            >
                            Can you explain the results ?

                            Of course it is narrow. It is a Reduced Optimization Set Compiler
                            (ROSC).

                            Jokes aside, obviously for you, the results aren't important but...
                            what?


                            --
                            jacob navia
                            jacob at jacob point remcomp point fr
                            logiciels/informatique

                            Comment

                            • Rui Maciel

                              #15
                              Re: Compiler optimizations

                              Randy Howard wrote:
                              Some provide switches to optimize for different "families" of
                              processors.  The problem is, it doesn't allow for any new hardware that
                              comes along after the compile is completed or after the compiler was
                              written.  Also, it doesn't allow for a binary to be used on multiple
                              hardware platforms from the same build while enjoying this special
                              attention.
                              That isn't exactly a compiler problem, is it?


                              Rui Maciel

                              Comment

                              Working...