public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: __builtin_expect for indirect function calls
@ 2008-01-06 20:42 Ross Ridge
  0 siblings, 0 replies; 13+ messages in thread
From: Ross Ridge @ 2008-01-06 20:42 UTC (permalink / raw)
  To: gcc

Mark Mitchell writes:
> What do people think?  Do we have the leeway to change this?

If it were just cases where using __builtin_expect is pointless that
would break, like function overloading and sizeof then I don't think
it would be a problem.  However, it would change behaviour when C++
conversion operators are used and I can see these being legitimately
used with __builtin_expect.  Something like:

	struct C {
		operator long();
	};

	int foo() {
		if (__builtin_expect(C(), 0))
			return 1;
		return 2;
	}

If cases like these are rare enough it's probably an acceptable change
if they give an error because the argument types don't match.

					Ross Ridge

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

* RE: __builtin_expect for indirect function calls
  2008-01-08 15:36               ` Dave Korn
@ 2008-01-08 15:51                 ` Dave Korn
  0 siblings, 0 replies; 13+ messages in thread
From: Dave Korn @ 2008-01-08 15:51 UTC (permalink / raw)
  To: 'Dave Korn', 'Mark Mendell', 'Mark Mitchell'
  Cc: Andrew_Pinski, 'gcc', 'Hans-Peter Nilsson',
	'Richard Guenther',
	Russell_Olsen, trevor_smigiel

On 08 January 2008 15:36, Dave Korn wrote:

> On 07 January 2008 21:15, Mark Mendell wrote:
> 
>> A question was raised:  Are side effects in the second parameter guaranteed
>> to be executed?  Is it valid for a compiler to ignore any side effects?
> 
>   That perked up my curiosity:
> 
> "  The value of C must be a compile-time constant.  "
> 
>   Can a compile-time constant have run-time side-effects?  I couldn't think
> up an example off the top of my head...


  Ah, I thought of one, nevermind, sorry for the noise...



    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

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

* RE: __builtin_expect for indirect function calls
  2008-01-07 21:15             ` Mark Mendell
@ 2008-01-08 15:36               ` Dave Korn
  2008-01-08 15:51                 ` Dave Korn
  0 siblings, 1 reply; 13+ messages in thread
From: Dave Korn @ 2008-01-08 15:36 UTC (permalink / raw)
  To: 'Mark Mendell', 'Mark Mitchell'
  Cc: Andrew_Pinski, 'gcc', 'Hans-Peter Nilsson',
	'Richard Guenther',
	Russell_Olsen, trevor_smigiel

On 07 January 2008 21:15, Mark Mendell wrote:

> A question was raised:  Are side effects in the second parameter guaranteed
> to be executed?  Is it valid for a compiler to ignore any side effects?

  That perked up my curiosity:

"  The value of C must be a compile-time constant.  "

  Can a compile-time constant have run-time side-effects?  I couldn't think up
an example off the top of my head...

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

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

* Re: __builtin_expect for indirect function calls
  2008-01-06 19:44           ` Mark Mitchell
@ 2008-01-07 21:15             ` Mark Mendell
  2008-01-08 15:36               ` Dave Korn
  0 siblings, 1 reply; 13+ messages in thread
From: Mark Mendell @ 2008-01-07 21:15 UTC (permalink / raw)
  To: Mark Mitchell
  Cc: Andrew_Pinski, gcc, Hans-Peter Nilsson, Richard Guenther,
	Russell_Olsen, trevor_smigiel

The frontends here would prefer to just implement __builtin_expect_call
(fp,foo), and leave __builtin_expect as it is now.  We don't see a need for
a polymorphic __builtin_expect, as we are worried about backwards
compatibility.

A question was raised:  Are side effects in the second parameter guaranteed
to be executed?  Is it valid for a compiler to ignore any side effects?

Mark Mendell

TOBEY Team Lead
IBM Toronto Laboratory, 8200 Warden Ave, Markham, Ontario, Canada, L6G 1C7
Tel:  905-413-3485    Tie:  313-3485    Internet:  mendell@ca.ibm.com


                                                                                                                     
  From:       Mark Mitchell <mark@codesourcery.com>                                                                  
                                                                                                                     
  To:         Richard Guenther <richard.guenther@gmail.com>                                                          
                                                                                                                     
  Cc:         trevor_smigiel@playstation.sony.com, Hans-Peter Nilsson <hp@bitrange.com>, gcc <gcc@gcc.gnu.org>,      
              Russell_Olsen@playstation.sony.com, Andrew_Pinski@playstation.sony.com, Mark Mendell/Toronto/IBM@IBMCA 
                                                                                                                     
  Date:       06/01/2008 02:42 PM                                                                                    
                                                                                                                     
  Subject:    Re: __builtin_expect for indirect function calls                                                       
                                                                                                                     





Richard Guenther wrote:

>> What do people think?  Do we have the leeway to change this?  Or should
>> we add __builtin_expect2?  Or add an -fno-polymorphic-builtin-expect?
>> Or...?
>
> I think we should simply make __builtin_expect polymorphic, but make sure
> to promote integral arguments with rank less than long to long.

I thought of that, but I hadn't suggested this idea because it seemed so
weird.  Promoting to int would not be odd, but promoting to long is
weird.  Anyhow, you're right; that's another option, and, despite
weirdness, plausible.  I can't think of a way in which it changes
current behavior, unless you call __builtin_expect with a long long, and
that's probably not going to do what you expect right now anyhow.

--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713


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

* Re: __builtin_expect for indirect function calls
  2008-01-05 10:40         ` Richard Guenther
@ 2008-01-06 19:44           ` Mark Mitchell
  2008-01-07 21:15             ` Mark Mendell
  0 siblings, 1 reply; 13+ messages in thread
From: Mark Mitchell @ 2008-01-06 19:44 UTC (permalink / raw)
  To: Richard Guenther
  Cc: trevor_smigiel, Hans-Peter Nilsson, gcc, Russell_Olsen,
	Andrew_Pinski, Mark Mendell

Richard Guenther wrote:

>> What do people think?  Do we have the leeway to change this?  Or should
>> we add __builtin_expect2?  Or add an -fno-polymorphic-builtin-expect?
>> Or...?
> 
> I think we should simply make __builtin_expect polymorphic, but make sure
> to promote integral arguments with rank less than long to long.

I thought of that, but I hadn't suggested this idea because it seemed so
weird.  Promoting to int would not be odd, but promoting to long is
weird.  Anyhow, you're right; that's another option, and, despite
weirdness, plausible.  I can't think of a way in which it changes
current behavior, unless you call __builtin_expect with a long long, and
that's probably not going to do what you expect right now anyhow.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

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

* Re: __builtin_expect for indirect function calls
  2008-01-05  5:44       ` Mark Mitchell
@ 2008-01-05 10:40         ` Richard Guenther
  2008-01-06 19:44           ` Mark Mitchell
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Guenther @ 2008-01-05 10:40 UTC (permalink / raw)
  To: Mark Mitchell
  Cc: trevor_smigiel, Hans-Peter Nilsson, gcc, Russell_Olsen,
	Andrew_Pinski, Mark Mendell

On Jan 5, 2008 6:43 AM, Mark Mitchell <mark@codesourcery.com> wrote:
> trevor_smigiel@playstation.sony.com wrote:
>
> > Currently, the prototype for __builtin_expect is
> >
> >     long __builtin_expect (long expression, long constant);
> >
> > Extending it to functions would change it to
> >
> >     T __builtin_expect (T expression, T expected);
>
> Yes, it really makes more sense for __builtin_expect to be polymorphic
> in this way anyhow.  I consider it a design wart that it's defined in
> terms of "long".  (For example, this means you can't use it for "long
> long"!)
>
> > Rather than the above definition, we could choose not to inspect the
> > context and just say:
> >     * T is the type of 'expression'
> >     * 'expected' is allowed to be a non-constant
> >
> > In this case I think there would only be compatibility issues with
> > unusual uses of __builtin_expect, for example, if it was being used in a
> > function argument and its type effected overload resolution.
>
> I think this is the abstractly right approach.  However, you're right
> that there are backwards-compatibility issues.  Another issue is that on
> platforms where "long" and "int" do not have the same width, something like:
>
>   sizeof(__builtin_expect(1, 1))
>
> will change its value.  And, the current prototype is documented in the
> manual.
>
> What do people think?  Do we have the leeway to change this?  Or should
> we add __builtin_expect2?  Or add an -fno-polymorphic-builtin-expect?
> Or...?

I think we should simply make __builtin_expect polymorphic, but make sure
to promote integral arguments with rank less than long to long.

Richard.

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

* Re: __builtin_expect for indirect function calls
  2008-01-03 23:36     ` trevor_smigiel
@ 2008-01-05  5:44       ` Mark Mitchell
  2008-01-05 10:40         ` Richard Guenther
  0 siblings, 1 reply; 13+ messages in thread
From: Mark Mitchell @ 2008-01-05  5:44 UTC (permalink / raw)
  To: trevor_smigiel
  Cc: Hans-Peter Nilsson, gcc, Russell_Olsen, Andrew_Pinski, Mark Mendell

trevor_smigiel@playstation.sony.com wrote:

> Currently, the prototype for __builtin_expect is
> 
>     long __builtin_expect (long expression, long constant);
> 
> Extending it to functions would change it to
> 
>     T __builtin_expect (T expression, T expected);

Yes, it really makes more sense for __builtin_expect to be polymorphic
in this way anyhow.  I consider it a design wart that it's defined in
terms of "long".  (For example, this means you can't use it for "long
long"!)

> Rather than the above definition, we could choose not to inspect the
> context and just say:
>     * T is the type of 'expression'
>     * 'expected' is allowed to be a non-constant
> 
> In this case I think there would only be compatibility issues with
> unusual uses of __builtin_expect, for example, if it was being used in a
> function argument and its type effected overload resolution.

I think this is the abstractly right approach.  However, you're right
that there are backwards-compatibility issues.  Another issue is that on
platforms where "long" and "int" do not have the same width, something like:

  sizeof(__builtin_expect(1, 1))

will change its value.  And, the current prototype is documented in the
manual.

What do people think?  Do we have the leeway to change this?  Or should
we add __builtin_expect2?  Or add an -fno-polymorphic-builtin-expect?
Or...?

Thanks,

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

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

* Re: __builtin_expect for indirect function calls
  2007-12-22  3:42 ` Hans-Peter Nilsson
  2007-12-26 19:10   ` Mark Mitchell
@ 2008-01-03 23:46   ` trevor_smigiel
  1 sibling, 0 replies; 13+ messages in thread
From: trevor_smigiel @ 2008-01-03 23:46 UTC (permalink / raw)
  To: Hans-Peter Nilsson
  Cc: gcc, Russell_Olsen, Mark Mitchell, Andrew_Pinski, Mark Mendell

> >
> > which returns the value of FP with the same type as FP, and tells the
> > compiler that PFP is the expected target of FP.  Trival examples:
> >
> >   typedef void (*fptr_t)(void);
> >
> >   extern void foo(void);
> >
> >   void
> >   call_fp (fptr_t fp)
> >   {
> >     /* Call the function pointed to by fp, but predict it as if it is
> >        calling foo() */
> >     __builtin_expect_call (fp, foo)();
> 
> __builtin_expect (fp, foo);  /* alt __builtin_expect (fp == foo, 1); */
> fp ();

> >   }
> >
> >   void
> >   call_fp_predicted (fptr_t fp, fptr_t predicted)
> >   {
> >     /* same as above but the function we are calling doesn't have to be
> >        known at compile time */
> >     __builtin_expect_call (fp, predicted)();
> 
> __builtin_expect (fp, predicted);
> fp();
> 
> I guess the information just isn't readily available in the
> preferred form when needed and *that* part could more or less
> simply be fixed?


The main reason I didn't like this is that in some other context 'fp'
could be any expression, potentially with side effects.  This would
require either special handling by the compiler, or the user would have
to be sure to write it such that the side effects only happen once.

Trevor

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

* Re: __builtin_expect for indirect function calls
  2007-12-26 19:10   ` Mark Mitchell
@ 2008-01-03 23:36     ` trevor_smigiel
  2008-01-05  5:44       ` Mark Mitchell
  0 siblings, 1 reply; 13+ messages in thread
From: trevor_smigiel @ 2008-01-03 23:36 UTC (permalink / raw)
  To: Mark Mitchell
  Cc: Hans-Peter Nilsson, gcc, Russell_Olsen, Andrew_Pinski, Mark Mendell

If possible, I agree it seems natural to extend __builtin_expect.  My
concern would be backwards compatibility.

Currently, the prototype for __builtin_expect is

    long __builtin_expect (long expression, long constant);

Extending it to functions would change it to

    T __builtin_expect (T expression, T expected);

With these additional semantics and restrictions:
    - when the return value is being used as a call expression:
      * T is the type of 'expression'
      * 'expected' is allowed to be a non-constant
    - when the return value is not being used as a call expression:
      * T is type 'long;
      * 'expected' must be a compile-time constant

Given the above definition, I don't think there is any backwards
compatibility issues because we are inspecting the context of the
use of __builtin_expect.

Rather than the above definition, we could choose not to inspect the
context and just say:
    * T is the type of 'expression'
    * 'expected' is allowed to be a non-constant

In this case I think there would only be compatibility issues with
unusual uses of __builtin_expect, for example, if it was being used in a
function argument and its type effected overload resolution. Or if the
argument was a float and was being implicitly converted to a long (with
a warning).  There would also be code which previously gave warnings but
does not with the extended __builtin_expect.

I'm ok with either of these definitions, if extending __builtin_expect
is the preferred way to go.

Are either of these definitions ok?  Or are there other ideas how to
define it?

Trevor

* Mark Mitchell <mark@codesourcery.com> [2008-01-03 12:12]:
> Hans-Peter Nilsson wrote:
> > On Mon, 17 Dec 2007, trevor_smigiel@playstation.sony.com wrote:
> >> When we can't hint the real target, we want to hint the most common
> >> target.   There are potentially clever ways for the compiler to do this
> >> automatically, but I'm most interested in giving the user some way to do
> >> it explicitly.  One possiblity is to have something similar to
> >> __builtin_expect, but for functions.  For example, I propose:
> >>
> >>   __builtin_expect_call (FP, PFP)
> > 
> > Is there a hidden benefit?  I mean, isn't this really
> > expressable using builtin_expect as-is, at least when it comes
> > to the syntax?  
> 
> That was my first thought as well.  Before we add __builtin_expect_call,
> I think there needs to be a justification of why this can't be done with
> __builtin_expect as-is.
> 
> -- 
> Mark Mitchell
> CodeSourcery
> mark@codesourcery.com
> (650) 331-3385 x713

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

* Re: __builtin_expect for indirect function calls
  2007-12-22  3:42 ` Hans-Peter Nilsson
@ 2007-12-26 19:10   ` Mark Mitchell
  2008-01-03 23:36     ` trevor_smigiel
  2008-01-03 23:46   ` trevor_smigiel
  1 sibling, 1 reply; 13+ messages in thread
From: Mark Mitchell @ 2007-12-26 19:10 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: trevor_smigiel, gcc, Russell_Olsen

Hans-Peter Nilsson wrote:
> On Mon, 17 Dec 2007, trevor_smigiel@playstation.sony.com wrote:
>> When we can't hint the real target, we want to hint the most common
>> target.   There are potentially clever ways for the compiler to do this
>> automatically, but I'm most interested in giving the user some way to do
>> it explicitly.  One possiblity is to have something similar to
>> __builtin_expect, but for functions.  For example, I propose:
>>
>>   __builtin_expect_call (FP, PFP)
> 
> Is there a hidden benefit?  I mean, isn't this really
> expressable using builtin_expect as-is, at least when it comes
> to the syntax?  

That was my first thought as well.  Before we add __builtin_expect_call,
I think there needs to be a justification of why this can't be done with
__builtin_expect as-is.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

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

* Re: __builtin_expect for indirect function calls
  2007-12-18  0:52 trevor_smigiel
  2007-12-18  2:27 ` Jonathan Adamczewski
@ 2007-12-22  3:42 ` Hans-Peter Nilsson
  2007-12-26 19:10   ` Mark Mitchell
  2008-01-03 23:46   ` trevor_smigiel
  1 sibling, 2 replies; 13+ messages in thread
From: Hans-Peter Nilsson @ 2007-12-22  3:42 UTC (permalink / raw)
  To: trevor_smigiel; +Cc: gcc, Russell_Olsen

On Mon, 17 Dec 2007, trevor_smigiel@playstation.sony.com wrote:
> When we can't hint the real target, we want to hint the most common
> target.   There are potentially clever ways for the compiler to do this
> automatically, but I'm most interested in giving the user some way to do
> it explicitly.  One possiblity is to have something similar to
> __builtin_expect, but for functions.  For example, I propose:
>
>   __builtin_expect_call (FP, PFP)

Is there a hidden benefit?  I mean, isn't this really
expressable using builtin_expect as-is, at least when it comes
to the syntax?  Like:

>
> which returns the value of FP with the same type as FP, and tells the
> compiler that PFP is the expected target of FP.  Trival examples:
>
>   typedef void (*fptr_t)(void);
>
>   extern void foo(void);
>
>   void
>   call_fp (fptr_t fp)
>   {
>     /* Call the function pointed to by fp, but predict it as if it is
>        calling foo() */
>     __builtin_expect_call (fp, foo)();

__builtin_expect (fp, foo);  /* alt __builtin_expect (fp == foo, 1); */
fp ();

>   }
>
>   void
>   call_fp_predicted (fptr_t fp, fptr_t predicted)
>   {
>     /* same as above but the function we are calling doesn't have to be
>        known at compile time */
>     __builtin_expect_call (fp, predicted)();

__builtin_expect (fp, predicted);
fp();

I guess the information just isn't readily available in the
preferred form when needed and *that* part could more or less
simply be fixed?

brgds, H-P

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

* Re: __builtin_expect for indirect function calls
  2007-12-18  0:52 trevor_smigiel
@ 2007-12-18  2:27 ` Jonathan Adamczewski
  2007-12-22  3:42 ` Hans-Peter Nilsson
  1 sibling, 0 replies; 13+ messages in thread
From: Jonathan Adamczewski @ 2007-12-18  2:27 UTC (permalink / raw)
  To: trevor_smigiel; +Cc: gcc, Ulrich Weigand, Russell_Olsen

trevor_smigiel@playstation.sony.com wrote:
> Are there any comments about the name, semantics, or usefulness of this
> extension?
>   

Sounds very useful for SPU code. I look forward to trying it out.


Toying with the idea, the following seems like a potentially useful C++
form of the proposed extension :

struct A {
virtual void foo();
};

struct B : public A {
virtual void foo();
};

A* a;

...

__builtin_expect_call (a->foo, B::foo)();



jonathan.

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

* __builtin_expect for indirect function calls
@ 2007-12-18  0:52 trevor_smigiel
  2007-12-18  2:27 ` Jonathan Adamczewski
  2007-12-22  3:42 ` Hans-Peter Nilsson
  0 siblings, 2 replies; 13+ messages in thread
From: trevor_smigiel @ 2007-12-18  0:52 UTC (permalink / raw)
  To: gcc; +Cc: Ulrich Weigand, Russell_Olsen

Hi,

I'm looking for comments on a possible GCC extensions described below.

For the target I'm interested in, Cell SPU, taken branches are only
predicted correctly by explicitly inserting a specific instructions (a
hint instruction) that says "the branch at address A is branching to
address B".  This allows the processor to prefetch the instructions at
B, potentially with no penalty.

For indirect function calls, the ideal case is we know the target soon
enough at run-time that the hint instruction simply specifies the real
target.  Soon enough means about 18 cycles before the execution of the
branch.  I don't have any numbers as to how often this happens, but
there are enough cases where it doesn't.

When we can't hint the real target, we want to hint the most common
target.   There are potentially clever ways for the compiler to do this
automatically, but I'm most interested in giving the user some way to do
it explicitly.  One possiblity is to have something similar to
__builtin_expect, but for functions.  For example, I propose:

  __builtin_expect_call (FP, PFP)

which returns the value of FP with the same type as FP, and tells the
compiler that PFP is the expected target of FP.  Trival examples:

  typedef void (*fptr_t)(void);

  extern void foo(void);

  void
  call_fp (fptr_t fp)
  {
    /* Call the function pointed to by fp, but predict it as if it is
       calling foo() */
    __builtin_expect_call (fp, foo)();
  }

  void
  call_fp_predicted (fptr_t fp, fptr_t predicted)
  {
    /* same as above but the function we are calling doesn't have to be
       known at compile time */
    __builtin_expect_call (fp, predicted)();
  }

I believe I can add this just for the SPU target without effecting
anything else, but it could be useful for other targets.

Are there any comments about the name, semantics, or usefulness of this
extension?

Thanks,
Trevor


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

end of thread, other threads:[~2008-01-08 15:51 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-06 20:42 __builtin_expect for indirect function calls Ross Ridge
  -- strict thread matches above, loose matches on Subject: below --
2007-12-18  0:52 trevor_smigiel
2007-12-18  2:27 ` Jonathan Adamczewski
2007-12-22  3:42 ` Hans-Peter Nilsson
2007-12-26 19:10   ` Mark Mitchell
2008-01-03 23:36     ` trevor_smigiel
2008-01-05  5:44       ` Mark Mitchell
2008-01-05 10:40         ` Richard Guenther
2008-01-06 19:44           ` Mark Mitchell
2008-01-07 21:15             ` Mark Mendell
2008-01-08 15:36               ` Dave Korn
2008-01-08 15:51                 ` Dave Korn
2008-01-03 23:46   ` trevor_smigiel

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