Board index » delphi » Re: Compiler optimisation

Re: Compiler optimisation


2005-01-21 12:15:13 PM
delphi105
Quote
Did I say it was good? :)
LOL
 
 

Re: Compiler optimisation

Dejan Stankovic <XXXX@XXXXX.COM>writes
<XXXX@XXXXX.COM>
Quote
If I am not mistaken Delphi doesn't have many optimizations.
You are quite mistaken. Optimized Delphi code runs at roughly 80-90% of the
speed of optimized VC++ code (except for heavy floating point code). It
wouldn't have that kind of speed if it had few optimizations, because I know
that the VC++ compile had quite a few optimizations.
The only one
Quote
I know it has is peephole. Intel's compilers, on the other hand, are
designed for high performance and have many different optimizations.
Intel's compilers are designed specifically for Intel chips, and like all C++
compilers/linkers lets you choose which optimizations apply. In Delphi you
don't get to choose which optimizations apply, you have to choose all or
nothing, but the optimizations are roughly the same as in a VC++ compile. By
the way, IIRC there was a big speed difference in VC++ code between the Intel
and AMD chips when you ran optimized code on them, with the AMD chips being
much much slower. That leads me to believe that the VC++ optimizer was
Intel-specific too, just like Intel's C++ compilers are.
It would be interesting to compare code speed for Intel C++ versus Delphi on
non-Intel chips, or on old Intel chips. Delphi produced code that was
optimized for Cyrix, Intel and AMD.
It is too bad Danny Thorpe is no longer posting here, as he would be able to
authoritatively set you straight on the issue of optimizations in Delphi
code.
--
***Free Your Mind***
Posted with JSNewsreader-BETA 0.9.4.369
 

Re: Compiler optimisation

Captain Jake writes:
Quote
It is too bad Danny Thorpe is no longer posting here, as he would be
able to authoritatively set you straight on the issue of
optimizations in Delphi code.
Hopefully he's coding a few more (optimizations) into the compiler
instead of being here :-)
Although he's probably knee deep in generics (pure speculation)
--
QC Client: www.alphalink.com.au/~jed/QC/
Blog: jedqc.blogspot.com/
Configure Delphi the way you want it to be:
www.alphalink.com.au/~jed/dcm.htm
Checkout my code central submissions for D2005
cc.borland.com/ccweb.exe/author
 

Re: Compiler optimisation

JED <XXXX@XXXXX.COM>writes
<XXXX@XXXXX.COM>
Quote
Hopefully he's coding a few more (optimizations) into the compiler
instead of being here :-)
I wish my D2005 would come so I could check out the inline directive. I'd
love to see if it speeds up my already really zippy newsreader.
Quote

Although he's probably knee deep in generics (pure speculation)
I hope he finds a way to do them that works at least as well as templates
worked in BC++. That would be sweet.
--
***Free Your Mind***
Posted with JSNewsreader-BETA 0.9.4.369
 

Re: Compiler optimisation

Captain Jake writes:
Quote
Dejan Stankovic <XXXX@XXXXX.COM>writes
<XXXX@XXXXX.COM>

>If I am not mistaken Delphi doesn't have many optimizations.


You are quite mistaken. Optimized Delphi code runs at roughly 80-90% of the
speed of optimized VC++ code (except for heavy floating point code). It
wouldn't have that kind of speed if it had few optimizations, because I know
that the VC++ compile had quite a few optimizations.
That is based on your experience and feeling and doesn't mean that was
result of optimizations.
Keep in mind that Delphi is one-pass compiler (apparently). This limits
number of possible optimizations to ones that do not operate on
intermediate code. Furthermore, there are three different levels of
intermediate languages upon which various optimizations are performed.
For your reference, following is, as complete as I could make it, list
of all optimizations that are used in todays compilers.
-Aggregation of global references
-Algebraic simplifications, including reassociations
-Basic-block and branch scheduling
-Branch optimizations and conditional moves
-Branch prediction
-Code hoisting
-Constant folding
-Control-flow optimizations
-Data prefetching
-Data-cache optimizations
-Dead-code elimination
-Global value numbering
-In-line expansion
-Induction-variable removal
-Induction-variable strength reduction
-Instruction prefetching
-Interprocedureal constant propagation
-Interprocedureal register allocation
-Intraprocedureal instruction cache optimization
-Leaf-routine optimization
-Linear-function test replacement
-Local and global common-subexpression elimination
-Local and global copy propagation
-Loop-invariant code motion
-Machine idioms
-Partial-redundancy elimination
-Procedure integration
-Procedure specialization and clonning
-Scalar replacement of aggregates
-Scalar replacement of array references
-Shrink wrapping
-Software pipelining, with loop unrolling, variable expansion, register
-renaming and hierarchical reduction
-Sparse conditional constant propagation
-Tail call optimization, including tail recursion elimination
-Tail merging
-Unnecessary bounds checking elimination
Keep in mind that even very good compilers implement only a small number
of those and that not all of those are speed optimizations.
Now going back to Delphi. It has always been a solid compiler with two
important features: it generates good code to begin with and it is
one-pass (lightning fast). If you ever wandered why C++ takes ages to
compile as compared to Delphi, this is the answer. A lot of that Delphi
has to thank to the language itself.
Quote
The only one

>I know it has is peephole. Intel's compilers, on the other hand, are
>designed for high performance and have many different optimizations.


Intel's compilers are designed specifically for Intel chips, and like all C++
compilers/linkers lets you choose which optimizations apply. In Delphi you
don't get to choose which optimizations apply, you have to choose all or
nothing, but the optimizations are roughly the same as in a VC++ compile. By
the way, IIRC there was a big speed difference in VC++ code between the Intel
and AMD chips when you ran optimized code on them, with the AMD chips being
much much slower. That leads me to believe that the VC++ optimizer was
Intel-specific too, just like Intel's C++ compilers are.
Most of the benefits come from pipeline optimizations. I read somewhere
that on MIPS you can get sevenfold increase in speed if you are careful
about pipeline.
As for AMD vs. Intel you might be right. If MS compiler does pipeline
optimization, this wouldn't work on AMD. I gues they go by the number of
usres that have Intel as opposed to AMD.
This brings me to another point. Delphi has never been designed to be
high-end compiler. For the purpose that people use it, it is more than
fast enough.
Quote
It would be interesting to compare code speed for Intel C++ versus Delphi on
non-Intel chips, or on old Intel chips. Delphi produced code that was
optimized for Cyrix, Intel and AMD.

It is too bad Danny Thorpe is no longer posting here, as he would be able to
authoritatively set you straight on the issue of optimizations in Delphi
code.

I'd like to set this straight. Especially, I would like to hear what
optimizations from the list above Delphi has.
Dejan
 

Re: Compiler optimisation

Quote
For your reference, following is, as complete as I could make it, list
of all optimizations that are used in todays compilers.
Quite a list. I will admit that the optimizer in the Delphi compiler is not
exactly state of the art, but in all fairness, it does exist and it does
improve the quality of the code considerably. You should have seen the code
that TurboPascal 3 emitted. It was horrible. Unfortunately, the optimizer
doesn't appear to have received much work, if at all (other than possibly
necessary support for new language constructs), since Delphi 2.
I don't know the precise meaning of all the optimizations you list, but I do
know that Delphi does at least some of:
Quote
-Algebraic simplifications
-Branch optimization
-Constant folding
-In-line expansion (explicit, requires D2005)
-Induction-variable removal
-Local common-subexpression elimination
-Machine idioms
In addition to those your list, it also does
- Redundant register store/load and load/reload elimination (not perfect but
way better than TP3)
- Unreachable code elimination (note that this is different from dead-code
elimination, which it doesn't do)
- Per
 

Re: Compiler optimisation

Per Larsen writes:
Quote
Quite a list. I will admit that the optimizer in the Delphi compiler is not
exactly state of the art, but in all fairness, it does exist and it does
improve the quality of the code considerably. You should have seen the code
that TurboPascal 3 emitted. It was horrible. Unfortunately, the optimizer
doesn't appear to have received much work, if at all (other than possibly
necessary support for new language constructs), since Delphi 2.
I have seen code TP3 emitted, that is where I started. Oh happy days :)
Quote
I don't know the precise meaning of all the optimizations you list, but I do
know that Delphi does at least some of:


>-Algebraic simplifications
>-Branch optimization
>-Constant folding
>-In-line expansion (explicit, requires D2005)
>-Induction-variable removal
>-Local common-subexpression elimination
>-Machine idioms
You do realise that all except Inlining and Machine idioms are
optimizations performed at the time of parsing and that pretty much
every compiler does them.
Quote


In addition to those your list, it also does

- Redundant register store/load and load/reload elimination (not perfect but
way better than TP3)
This is job for peep-hole optimizer. It looks at generated code
(sometimes intermediate code) through a 2-3 instruction window and then
eliminates redundant cases. I didn't check Andrews statement (few posts
before this one), but if it is correct than it doesn't do very well here.
Quote
- Unreachable code elimination (note that this is different from dead-code
elimination, which it doesn't do)
Only partially. While:
If False Then Statement;
is correctly eliminated, this is not (D6):
Exit;//Or Break or Continue
Statement;
I would like to say that I do like Delphi and I don't mind too much it's
lackings, but we need to face reality. Delphi is a bit outdated compiler
(by Delphi here I mean Win32 compiler, not .NET).
Quote
- Per


Best regards,
Dejan
 

Re: Compiler optimisation

On Fri, 21 Jan 2005 18:30:00 +1100, Dejan Stankovic
<XXXX@XXXXX.COM>writes:
Quote
Keep in mind that Delphi is one-pass compiler (apparently).
Yes, and no. Take a look at Danny's post:
tinyurl.com/6plmf
(same as:)
groups.google.com/groups&lr=&selm=3f2dd72a%241%40newsgroups.borland.com&rnum=1
--
Anders Isaksson, Sweden
BlockCAD: w1.161.telia.com/~u16122508/proglego.htm
Gallery: w1.161.telia.com/~u16122508/gallery/index.htm
 

Re: Compiler optimisation

"Dejan Stankovic" <XXXX@XXXXX.COM>writes
Quote
You do realise that all except Inlining and Machine idioms are
optimizations performed at the time of parsing and that pretty much
every compiler does them.
I don't know whether Delphi does them precisely while parsing or later, but
yes these are obviously relatively basic optimizations.
Quote
Delphi is a bit outdated compiler (by Delphi here I mean Win32 compiler,
not .NET).
Delphi is not a heavily optimizing compiler. I, like some others, would like
to see that change, but Borland's emphasis in the past has always been
compile speed over final code speed/size, and granted some of the analysis
required by the more advanced optimization algorithms does take significant
time to do - even on today's hardware.
However, saying that the compiler is outdated is missing the mark in this
context, IMHO. After all, the theory behind nearly all of the optimizations
listed has been known for 30+ years.
- Per
 

Re: Compiler optimisation

Anders Isaksson writes:
Quote
>Keep in mind that Delphi is one-pass compiler (apparently).

Yes, and no. Take a look at Danny's post:

(same as:)
groups.google.com/groups&lr=&selm=3f2dd72a%241%40newsgroups.borland.com&rnum=1

Fair enough, I take it back. Thanks for the link.
 

Re: Compiler optimisation

Quote
It is too bad Danny Thorpe is no longer posting here, as he would be able to
authoritatively set you straight on the issue of optimizations in Delphi
code.
I hadn't seen Danny here in a bit, but what's this about "no longer
posting here"--like he's said "I'm outta here" or something like that?
 

Re: Compiler optimisation

Quote
However, saying that the compiler is outdated is missing the mark in this
context, IMHO. After all, the theory behind nearly all of the optimizations
listed has been known for 30+ years.
Well if it stays let's say within 10-20% of what the other good
compilers do performance wise then it is fine. But they
should certainly take care of floating point that sucks.
Kostya
 

Re: Compiler optimisation

"Paul Dolen" <XXXX@XXXXX.COM>writes
Quote
>It is too bad Danny Thorpe is no longer posting here, as he would be able
>to
>authoritatively set you straight on the issue of optimizations in Delphi
>code.

I hadn't seen Danny here in a bit, but what's this about "no longer
posting here"--like he's said "I'm outta here" or something like that?
No, just hasn't been around.
 

Re: Compiler optimisation

Dejan Stankovic writes:
Quote

Keep in mind that Delphi is one-pass compiler (apparently).
Incorrect. The Delphi compiler is a one-pass, top-down parser with
incremental code generation.
Quote
This
limits number of possible optimizations to ones that do not operate
on intermediate code.
True, but not applicable to Delphi. The Delphi compiler does use
intermediate representations and multi-stage AST tree t{*word*220}
optimizations.
Quote

For your reference, following is, as complete as I could make it,
list of all optimizations that are used in todays compilers.

I've added notations for which of these the Delphi compiler implements.
Quote
-Aggregation of global references
No, if this is referring to global constant folding (strings, primarily)
Quote
-Algebraic simplifications, including reassociations
Yes, but to a limited degree. We don't replace division with multiply
by reciprocal, for example, because of concerns about loss of precision
at the edges.
Quote
-Basic-block and branch scheduling
No.
Quote
-Branch optimizations
Yes.
Quote
and conditional moves
No.
Quote
-Branch prediction
Static.
Quote
-Code hoisting
No.
Quote
-Constant folding
Yes.
Quote
-Control-flow optimizations
Unclear.
Quote
-Data prefetching
No.
Quote
-Data-cache optimizations
No.
Quote
-Dead-code elimination
Yes.
Quote
-Global value numbering
No.
Quote
-In-line expansion
Yes.
Quote
-Induction-variable removal
Yes.
Quote
-Induction-variable strength reduction
Yes.
Quote
-Instruction prefetching
No.
Quote
-Interprocedureal constant propagation
Unclear. A constant is constant everywhere, no?
Quote
-Interprocedureal register allocation
No.
Quote
-Intraprocedureal instruction cache optimization
No.
Quote
-Leaf-routine optimization
No.
Quote
-Linear-function test replacement
Unclear.
Quote
-Local and global common-subexpression elimination
Yes.
Quote
-Local and global copy propagation
No.
Quote
-Loop-invariant code motion
Yes.
Quote
-Machine idioms
No.
Quote
-Partial-redundancy elimination
Yes. (for pointer derefs)
Quote
-Procedure integration
Unclear.
Quote
-Procedure specialization and clonning
No.
Quote
-Scalar replacement of aggregates
-Scalar replacement of array references
If this refers to constant expression evaluation, yes.
Quote
-Shrink wrapping
??
Quote
-Software pipelining, with loop unrolling, variable expansion,
register -renaming and hierarchical reduction
Codegen templates are hand-tuned to favor instruction scheduling for
the U and V pipelines of the Pentium family of processors.
Quote
-Sparse conditional constant propagation
No.
Quote
-Tail call optimization, including tail recursion elimination
No.
Quote
-Tail merging
No.
Quote
-Unnecessary bounds checking elimination
Yes.
Quote

Keep in mind that even very good compilers implement only a small
number of those and that not all of those are speed optimizations.

Correct. The tail recursion optimizations in your list, for example,
are astronomically rare in the wild. Such optimizations typically only
kick in if you write you recursive routines "just so" which means only
the illuminati will benefit by it.
The mantra for Delphi optimizations is to make everyday code work
better. We don't the luxury of chasing after miraculous optimizations
that almost never work. I leave that to the grad students. ;>
Quote
Now going back to Delphi. It has always been a solid compiler with
two important features: it generates good code to begin with and it
is one-pass (lightning fast). If you ever wandered why C++ takes ages
to compile as compared to Delphi, this is the answer. A lot of that
Delphi has to thank to the language itself.

True, except for the one-pass assertion. The codegen phase is
multi-pass intermediate node tree traversals and pruning. We just do
it many times faster than the more traditional compiler models that
were originally concieved for batch processing of punched cards.
Quote

This brings me to another point. Delphi has never been designed to be
high-end compiler. For the purpose that people use it, it is more
than fast enough.

Correct. It could be said that Delphi is fast enough to lead some
folks to believe it should be the best at every computational scenario.
Delphi's objective is to be the most productive software development
tool for a very broad audience of developers. The compiler is one
small part of that.
Quote
>
>It is too bad Danny Thorpe is no longer posting here, as he would
>be able to authoritatively set you straight on the issue of
>optimizations in Delphi code.
>

I wish you people would quit invoking my name. Popping in here with a
big puff of smoke and a few lightning bolts is fun and all, but it
takes days to get the smell of sulfur out of my hair.
-Danny
--
Delphi Compiler Core: blogs.borland.com/dcc
 

Re: Compiler optimisation

Paul Dolen writes:
Quote
I hadn't seen Danny here in a bit, but what's this about "no longer
posting here"--like he's said "I'm outta here" or something like that?
No, more like "I have work to do."
--
Delphi Compiler Core: blogs.borland.com/dcc