public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* potential simple loop optimization assistance strategy?
@ 2005-07-01 15:51 Paul Schlie
  2005-07-01 16:24 ` Gabriel Dos Reis
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Paul Schlie @ 2005-07-01 15:51 UTC (permalink / raw)
  To: GCC Development

As in general it seems that as the compiler knows what it needs to know to
enable ideal loop optimization, why not simply have it assert: if it knew x,
then it could do y?  For example, if given something like:

  for (i = x; i < y; i++){ ... }

Where it may not be known statically if x < y, or what (y - x) % N may be;
therefore possibly difficult to ideally optimize.  Where then instead of
assuming something that may not be factual by default, it could generate a
warning indicating that the loop may be further optimized if it knew that
x < y, and/or (y - x) % N == 0.  Where then the programmer could then choose
to warrant it by preconditioning the loop:

  assert((x < y) && ((y - x) % 4)); // which could throw an exception.

  for ((i = x; i < y; i++){ ... }

Where although it would require run-time code, it's overhead should be
insignificant if the benefit of the optimization were truly significant;
thereby enabling the programmer to help the compiler help himself, without
the introducing potentially arbitrary behaviors, by explicitly trapping them
instead.

Where if the programmer didn't add an assertion, or explicitly enable a
potentially unsafe optimization, then the compiler should not presume
anything other than what it truly factually knows, which may include
optimizations based on whether the target traps or wraps overflow, etc.

(it may even be reasonable for the compiler to optionally insert run-time
assertions automatically to enable the deterministic trapping of conditions
which may be inconsistent with the optimizations assumptions?)


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 15:51 potential simple loop optimization assistance strategy? Paul Schlie
@ 2005-07-01 16:24 ` Gabriel Dos Reis
  2005-07-01 17:15   ` Paul Schlie
  2005-07-01 16:27 ` Devang Patel
  2005-07-01 18:16 ` Giovanni Bajo
  2 siblings, 1 reply; 11+ messages in thread
From: Gabriel Dos Reis @ 2005-07-01 16:24 UTC (permalink / raw)
  To: Paul Schlie; +Cc: GCC Development

Paul Schlie <schlie@comcast.net> writes:

| As in general it seems that as the compiler knows what it needs to know to
| enable ideal loop optimization, why not simply have it assert: if it knew x,
| then it could do y?  For example, if given something like:
| 
|   for (i = x; i < y; i++){ ... }
| 
| Where it may not be known statically if x < y, or what (y - x) % N may be;
| therefore possibly difficult to ideally optimize.  Where then instead of
| assuming something that may not be factual by default, it could generate a
| warning indicating that the loop may be further optimized if it knew that
| x < y, and/or (y - x) % N == 0.  Where then the programmer could then choose
| to warrant it by preconditioning the loop:
| 
|   assert((x < y) && ((y - x) % 4)); // which could throw an exception.
| 
|   for ((i = x; i < y; i++){ ... }
| 
| Where although it would require run-time code, it's overhead should be
| insignificant if the benefit of the optimization were truly significant;

I would not predicate the transformation on that assumption
(negligible cost of assertion).  That could happen in an inner tight
loop. 

What we need is a balance that does not require too much of work from
the compiler -- because otherwise, it won't happen.

-- Gaby

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 15:51 potential simple loop optimization assistance strategy? Paul Schlie
  2005-07-01 16:24 ` Gabriel Dos Reis
@ 2005-07-01 16:27 ` Devang Patel
  2005-07-01 18:16 ` Giovanni Bajo
  2 siblings, 0 replies; 11+ messages in thread
From: Devang Patel @ 2005-07-01 16:27 UTC (permalink / raw)
  To: Paul Schlie; +Cc: GCC Development

On Jul 1, 2005, at 8:51 AM, Paul Schlie wrote:

> As in general it seems that as the compiler knows what it needs to  
> know to
> enable ideal loop optimization, why not simply have it assert: if  
> it knew x,
> then it could do y?  For example, if given something like:
>
>   for (i = x; i < y; i++){ ... }
>
> Where it may not be known statically if x < y, or what (y - x) % N  
> may be;
> therefore possibly difficult to ideally optimize.  Where then  
> instead of
> assuming something that may not be factual by default, it could  
> generate a
> warning indicating that the loop may be further optimized if it  
> knew that
> x < y, and/or (y - x) % N == 0.  Where then the programmer could  
> then choose
> to warrant it by preconditioning the loop:
>
>   assert((x < y) && ((y - x) % 4)); // which could throw an exception.
>
>   for ((i = x; i < y; i++){ ... }
>
> Where although it would require run-time code, it's overhead should be
> insignificant if the benefit of the optimization were truly  
> significant;
> thereby enabling the programmer to help the compiler help himself,  
> without
> the introducing potentially arbitrary behaviors, by explicitly  
> trapping them
> instead.
>
> Where if the programmer didn't add an assertion, or explicitly  
> enable a
> potentially unsafe optimization, then the compiler should not presume
> anything other than what it truly factually knows, which may include
> optimizations based on whether the target traps or wraps overflow,  
> etc.
>
> (it may even be reasonable for the compiler to optionally insert  
> run-time
> assertions automatically to enable the deterministic trapping of  
> conditions
> which may be inconsistent with the optimizations assumptions?)
>

Various compilers support #pragma to inform compiler about approx.  
number
of loop iterations.


-
Devang

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 16:24 ` Gabriel Dos Reis
@ 2005-07-01 17:15   ` Paul Schlie
  0 siblings, 0 replies; 11+ messages in thread
From: Paul Schlie @ 2005-07-01 17:15 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: GCC Development

> From: Gabriel Dos Reis <gdr@integrable-solutions.net>
> | Paul Schlie <schlie@comcast.net> writes:
> | As in general it seems that as the compiler knows what it needs to know to
> | enable ideal loop optimization, why not simply have it assert: if it knew x,
> | then it could do y?  For example, if given something like:
> | 
> |   for (i = x; i < y; i++){ ... }
> | 
> | Where it may not be known statically if x < y, or what (y - x) % N may be;
> | therefore possibly difficult to ideally optimize.  Where then instead of
> | assuming something that may not be factual by default, it could generate a
> | warning indicating that the loop may be further optimized if it knew that
> | x < y, and/or (y - x) % N == 0.  Where then the programmer could then choose
> | to warrant it by preconditioning the loop:
> | 
> |   assert((x < y) && ((y - x) % 4)); // which could throw an exception.
> | 
> |   for ((i = x; i < y; i++){ ... }
> | 
> | Where although it would require run-time code, it's overhead should be
> | insignificant if the benefit of the optimization were truly significant;
> 
> I would not predicate the transformation on that assumption
> (negligible cost of assertion).  That could happen in an inner tight
> loop. 
> 
> What we need is a balance that does not require too much of work from
> the compiler -- because otherwise, it won't happen.

Understood, but can't help but wonder how many nested loops realalisticly
have nested data dependancies; but none the less, I guess all it takes is
one important one to justify worrying about it. And agree it would be nice
if static assertions could be made in some reasonable way embedded in the
code.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 15:51 potential simple loop optimization assistance strategy? Paul Schlie
  2005-07-01 16:24 ` Gabriel Dos Reis
  2005-07-01 16:27 ` Devang Patel
@ 2005-07-01 18:16 ` Giovanni Bajo
  2005-07-01 18:22   ` Diego Novillo
  2 siblings, 1 reply; 11+ messages in thread
From: Giovanni Bajo @ 2005-07-01 18:16 UTC (permalink / raw)
  To: Paul Schlie; +Cc: gcc

Paul Schlie <schlie@comcast.net> wrote:

> Where then the programmer could then choose
> to warrant it by preconditioning the loop:
>
>   assert((x < y) && ((y - x) % 4)); // which could throw an exception.
>
>   for ((i = x; i < y; i++){ ... }

There has been some opposition in the past about allowing conditions in
asserts to be used as hints to the optimizers. In fact, I would like to know
if there is a current statement of purpose about this. That is, would there
be strong oppositions to patches doing this?
-- 
Giovanni Bajo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 18:16 ` Giovanni Bajo
@ 2005-07-01 18:22   ` Diego Novillo
  2005-07-01 18:26     ` Giovanni Bajo
  0 siblings, 1 reply; 11+ messages in thread
From: Diego Novillo @ 2005-07-01 18:22 UTC (permalink / raw)
  To: Giovanni Bajo; +Cc: Paul Schlie, gcc

On Fri, Jul 01, 2005 at 08:16:19PM +0200, Giovanni Bajo wrote:
> Paul Schlie <schlie@comcast.net> wrote:
> 
> > Where then the programmer could then choose
> > to warrant it by preconditioning the loop:
> >
> >   assert((x < y) && ((y - x) % 4)); // which could throw an exception.
> >
> >   for ((i = x; i < y; i++){ ... }
> 
> There has been some opposition in the past about allowing conditions in
> asserts to be used as hints to the optimizers. In fact, I would like to know
> if there is a current statement of purpose about this. That is, would there
> be strong oppositions to patches doing this?
>
VRP naturally takes advantage of assert (though not in some
occassions involving unsigned types).  Try:

#include <assert.h>

foo (int i)
{
  assert (i != 0);
  if (i == 0)
    return 3;

  return 2;
}


Diego.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 18:22   ` Diego Novillo
@ 2005-07-01 18:26     ` Giovanni Bajo
  2005-07-01 19:42       ` Paul Schlie
       [not found]       ` <m3k6ka6po1.fsf@localhost.localdomain>
  0 siblings, 2 replies; 11+ messages in thread
From: Giovanni Bajo @ 2005-07-01 18:26 UTC (permalink / raw)
  To: Diego Novillo; +Cc: Paul Schlie, gcc

Diego Novillo <dnovillo@redhat.com> wrote:

>> There has been some opposition in the past about allowing conditions in
>> asserts to be used as hints to the optimizers. In fact, I would like to
>> know if there is a current statement of purpose about this. That is,
would
>> there be strong oppositions to patches doing this?
>>
> VRP naturally takes advantage of assert (though not in some
> occassions involving unsigned types).  Try:
>
> #include <assert.h>
>
> foo (int i)
> {
>   assert (i != 0);
>   if (i == 0)
>     return 3;
>
>   return 2;
> }

Agreed, but my point is whether we can do that when NDEBUG is defined.
-- 
Giovanni Bajo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-01 18:26     ` Giovanni Bajo
@ 2005-07-01 19:42       ` Paul Schlie
       [not found]       ` <m3k6ka6po1.fsf@localhost.localdomain>
  1 sibling, 0 replies; 11+ messages in thread
From: Paul Schlie @ 2005-07-01 19:42 UTC (permalink / raw)
  To: Giovanni Bajo, Diego Novillo; +Cc: gcc

> From: Giovanni Bajo <rasky@develer.com>
>> Diego Novillo <dnovillo@redhat.com> wrote:
>>> There has been some opposition in the past about allowing conditions in
>>> asserts to be used as hints to the optimizers. In fact, I would like to
>>> know if there is a current statement of purpose about this. That is,
> would
>>> there be strong oppositions to patches doing this?
>>> 
>> VRP naturally takes advantage of assert (though not in some
>> occassions involving unsigned types).  Try:
>> 
>> #include <assert.h>
>> 
>> foo (int i)
>> {
>>   assert (i != 0);
>>   if (i == 0)
>>     return 3;
>> 
>>   return 2;
>> }
> 
> Agreed, but my point is whether we can do that when NDEBUG is defined.

Might be nice to have both flavors, or take a parameter which indicates
if it's meant to be a static assertion, or throw a named runtime exception;
where the static assertion inhibit code generation simply inhibits code
generation somehow? as their requirement is not mutually exclusive.




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
       [not found]       ` <m3k6ka6po1.fsf@localhost.localdomain>
@ 2005-07-02  9:18         ` Giovanni Bajo
  2005-07-02 15:39           ` Michael Veksler
  0 siblings, 1 reply; 11+ messages in thread
From: Giovanni Bajo @ 2005-07-02  9:18 UTC (permalink / raw)
  To: tromey; +Cc: gcc

Tom Tromey <tromey@redhat.com> wrote:

> Giovanni> Agreed, but my point is whether we can do that when NDEBUG
> Giovanni> is defined.
>
> I thought when NDEBUG is defined, assert expands to something like
> '(void) 0' -- the original expression is no longer around.


Yes, but the condition is still morally true in the code. NDEBUG is meant to
speed up the generated code, and it's actually a pity that instead it
*disables* some optimizations because we don't see the condition anymore. My
suggestion is that assert with NDEBUG might expand to something like:

if (condition)
   unreachable();

where unreachable is a function call marked with a special attribute saying
that execution can never get there. This way the run-time check is removed from
the code, but the range information can still be propagated and used.

Notice that such an attribute would be needed in the first place for
gcc_unreachable() in our own sources. Right now we expand it to gcc_assert(0),
but we could do much better with a special attribute.

Giovanni Bajo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-02  9:18         ` Giovanni Bajo
@ 2005-07-02 15:39           ` Michael Veksler
  2005-07-02 18:58             ` Giovanni Bajo
  0 siblings, 1 reply; 11+ messages in thread
From: Michael Veksler @ 2005-07-02 15:39 UTC (permalink / raw)
  To: Giovanni Bajo; +Cc: gcc, tromey





Giovanni Bajo wrote on 02/07/2005 12:18:00:
>
> Yes, but the condition is still morally true in the code. NDEBUG is meant
to
> speed up the generated code, and it's actually a pity that instead it
> *disables* some optimizations because we don't see the condition anymore.
My
> suggestion is that assert with NDEBUG might expand to something like:
>
> if (condition)
>    unreachable();
>
> where unreachable is a function call marked with a special attribute
saying
> that execution can never get there. This way the run-time check is
> removed from
> the code, but the range information can still be propagated and used.
>
> Notice that such an attribute would be needed in the first place for
> gcc_unreachable() in our own sources. Right now we expand it to
gcc_assert(0),
> but we could do much better with a special attribute.

I always thought that when NDEBUG is set, then assert(x) should
have no side effects. So if "condition" contains any side effect,
or potential side effect (e.g. through function call), then the
compiler should not generate the code for "condition".

  Michael

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: potential simple loop optimization assistance strategy?
  2005-07-02 15:39           ` Michael Veksler
@ 2005-07-02 18:58             ` Giovanni Bajo
  0 siblings, 0 replies; 11+ messages in thread
From: Giovanni Bajo @ 2005-07-02 18:58 UTC (permalink / raw)
  To: Michael Veksler; +Cc: gcc, tromey

Michael Veksler <VEKSLER@il.ibm.com> wrote:

> have no side effects. So if "condition" contains any side effect,
> or potential side effect (e.g. through function call), then the
> compiler should not generate the code for "condition".


Right. Thus we can find a way to avoid generating e.g. function calls or
similar things, and get information from the most basic conditions.

Giovanni Bajo

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-07-02 18:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-01 15:51 potential simple loop optimization assistance strategy? Paul Schlie
2005-07-01 16:24 ` Gabriel Dos Reis
2005-07-01 17:15   ` Paul Schlie
2005-07-01 16:27 ` Devang Patel
2005-07-01 18:16 ` Giovanni Bajo
2005-07-01 18:22   ` Diego Novillo
2005-07-01 18:26     ` Giovanni Bajo
2005-07-01 19:42       ` Paul Schlie
     [not found]       ` <m3k6ka6po1.fsf@localhost.localdomain>
2005-07-02  9:18         ` Giovanni Bajo
2005-07-02 15:39           ` Michael Veksler
2005-07-02 18:58             ` Giovanni Bajo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).