Compiler optimizations

S

sammy

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.
 
J

jameskuyper

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.
 
U

user923005

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.
 
C

CBFalconer

sammy wrote:
...

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.
 
K

Kelsey Bjarnason

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.
 
J

Joachim Schmitz

Kelsey said:
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
 
R

Rui Maciel

Kelsey said:
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
 
M

Malcolm McLean

Kelsey Bjarnason said:
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.
 
J

jacob navia

sammy said:
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
 
S

Stephen Sprunk

sammy said:
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
 
R

Randy Howard

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. ;-)
 
R

Randy Howard

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.
 
R

Randy Howard

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?
 
J

jacob navia

Randy said:
<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?
 
R

Rui Maciel

Randy said:
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
 
U

user923005

How can it possibly know which computer(s) you will install and run it
on after it is compiled?  

GCC aside:
-march is supposed to be a promise of that. If you run it on
something else then you won't get the sort of performance you were
hoping for. Many other compilers have this same sort of effect (even
producing code that will only run on certain CPUs in some instances).
Not every program is something for you to play with for a bit in ~/src
then forget about.  ;-)

Rats. How deflating.
 
C

Chris Dollin

Randy said:
How can it possibly know which computer(s) you will install and run it
on after it is compiled?

The optimizer could run on the target machine, so "this one"
would be the appropriate answer.

[Warning: mere possibility isn't evidence of implementation.]
 
J

jacob navia

Chris said:
Randy said:
How can it possibly know which computer(s) you will install and run it
on after it is compiled?

The optimizer could run on the target machine, so "this one"
would be the appropriate answer.

[Warning: mere possibility isn't evidence of implementation.]

Shipping the optimizer with your application?
 
R

Randy Howard

GCC aside:
-march is supposed to be a promise of that. If you run it on
something else then you won't get the sort of performance you were
hoping for. Many other compilers have this same sort of effect (even
producing code that will only run on certain CPUs in some instances).

I think you missed what I was saying there. You can optimize with
something like -march for a specific hardware type, but when you move
that binary to other machines it isn't a promise of anything, it may
not even run properly.
 
C

CJ

Chris said:
The optimizer could run on the target machine, so "this one"
would be the appropriate answer.

[Warning: mere possibility isn't evidence of implementation.]

Shipping the optimizer with your application?

Yes, though that rather depends on shipping the source code with your
application too, so it wouldn't be much use for closed-source programs
like lcc-win for example...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top