Re: looking for thoughts on the philosophy of optimizing compilers
- From: Spiros Bousbouras <spibou@xxxxxxxxx>
- Date: Wed, 6 May 2009 12:02:34 -0700 (PDT)
On 6 May, 19:25, Chris Friesen <cbf...@xxxxxxxxxxxxx> wrote:
Spiros Bousbouras wrote:
On 6 May, 18:02, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:
Chris Friesen wrote:
Here are a couple of examples:Good for the compiler! say I.
1) Removal of a hardcoded busy-wait. This loop has no side effects, so
the compiler removes it entirely.
for (int i=0;i<1000000;i++)
continue;
Bad for the programmer say I. I have encountered situations
where the only way to get the exact delay I wanted was through
such loops. But I wouldn't use the continue.
On most processors there should be a clock cycle counter or timestamp
counter that could be read instead of relying on the compiler to not
optimize.
How many desktop platforms have you encountered where you have
access to such a counter from a C programme possibly using
operating system specific functions? <OOT> On POSIX at least the
function with the smallest time granularity I know of is
nanosleep() and it hasn't always given me the precise delay I
wanted. POSIX adds a lot to the C standard. If it still doesn't
give you direct access to a clock cycle counter it goes to show
that sometimes you need such loops, as ugly as they are. </OOT>
Code like this *should* be removed,
not only by the optimizer but also by any programmer who sees it in
the source.
Certainly not by the optimizer unless we're talking about some
seriously advanced AI. A human programmer should investigate
further before removing it.
The code has no side effects visible to the rest of the program (as
defined by the language model), so the compiler is free to remove it.
According to the standard yes, it's free to remove it. But when
Eric said that it "*should* be removed" I took it to mean from a
QOI point of view and I was replying in the same spirit. From a
QOI point of view I say it should *not* be removed.
In fact, a sufficiently advanced CPU could recognize that there are no
side effects and optimize it away.
Well, I hope if such an advanced CPU ever gets created it should
have instructions to specify that some code should not be optimised
away.
Arguably a human programmer should replace it with code that behaves as
intended rather than removing it or leaving it.
A human programmer should try and decide if the intention of the
previous programmer was to place a delay at that point in the
code. If that was indeed the intention then the code is correct
as written and it should neither be removed nor modified (unless
of course the new programmer can think of a better way to
achieve the same effect).
.
- Follow-Ups:
- Re: looking for thoughts on the philosophy of optimizing compilers
- From: Eric Sosman
- Re: looking for thoughts on the philosophy of optimizing compilers
- From: Chris Friesen
- Re: looking for thoughts on the philosophy of optimizing compilers
- References:
- looking for thoughts on the philosophy of optimizing compilers
- From: Chris Friesen
- Re: looking for thoughts on the philosophy of optimizing compilers
- From: Eric Sosman
- Re: looking for thoughts on the philosophy of optimizing compilers
- From: Spiros Bousbouras
- Re: looking for thoughts on the philosophy of optimizing compilers
- From: Chris Friesen
- looking for thoughts on the philosophy of optimizing compilers
- Prev by Date: Re: looking for thoughts on the philosophy of optimizing compilers
- Next by Date: Re: looking for thoughts on the philosophy of optimizing compilers
- Previous by thread: Re: looking for thoughts on the philosophy of optimizing compilers
- Next by thread: Re: looking for thoughts on the philosophy of optimizing compilers
- Index(es):
Relevant Pages
|