public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: std::pow implementation
@ 2003-07-30 13:56 Robert Dewar
  2003-07-30 14:04 ` Richard Guenther
  2003-07-30 14:08 ` Gabriel Dos Reis
  0 siblings, 2 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 13:56 UTC (permalink / raw)
  To: aoliva, gdr; +Cc: gcc, rguenth, s.bosscher

> Defining a function inline is not a careless action.  If inlining
> isn't profitable, the programmer will profile et remove the inline
> definition.  You don't know better than the programmer.

This is often incorrect, because often code is written to be portable, and
the decision on what is profitable to inline is target dependent. Furthermore
deciding whether something is profitable to inline on a given target requires
detailed knowledge of the target architecture, knowledge that very few 
programmers have these days.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 22:57 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 22:57 UTC (permalink / raw)
  To: hahn, tm_gccmail; +Cc: dewar, gcc

> > pretty hard to do for the great majority of C++ (or Ada for that matter)
> > programmers who have not the slightest awareness of machine language or
> > its implications.
> 
> so since some programmers are imperfect,
> none of them should get nice sharp tools?  please, no!
> 
> C/C++, at least, have never shied from enough-rope features.
> 
> regards, mark hahn.

Well I am not sure it is a matter of rope here. The idea that all class
definitions in C++ should be regarded as potentially highly target
instruction set dependent since the programmer is expected to analyze
for a particular architecture what should and should not be inlined
seems a bit gruesome.

There are times when programming oblivious to the details of the target
architecture is exactly the right thing to do, and that should surely be
most of the time when using a language like C++ (otherwise why not write
in ASM to start with).

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 18:42 Robert Dewar
  2003-08-04 18:46 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 18:42 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aoliva, bernds, gcc, jbuck, rguenth, s.bosscher

> I did appeal to two sources: both the ISO standard and "The Design and
> Evolution of C++".  I included the ISO standard definition because it
> gives the wording on the current C++ definition and it is the result
> long elaboration on wordings that took many resources, just to make
> sure that thee substitution meaning is conveyed.  I did also appeal
> to the D&E because it does give references to the raison d'etre of
> inline in C++.  You might choose to ignore that C++ is an "evolved"
> language and ignore its history, but that is a not mistake I would
> like GCC to make.  C++ simply is not Ada.

The standard is a stand alone document, approved by ISO as such. It's meaning
if it has any meaning must be able to be deduced from its intrinsic content
like any other language standard. You cannot apply "history" to its
interpretation. This is just as true of Ada as of C++ (Ada has a long,
over 20 year, history too you know), and it is aqlso true that an Ada
compiler writer must be fully informed by this history.

For example, when we write

   type x (A : Natural) is record
      Y : String (1 .. A);
   end record;

in Ada, there is a difficult and significant question of how this is
implemented, and whether a given implementation will accept this particular
declaration. The Ada standard has absolutely nothing to say on this issue,
since it is one of implementation and code generation rather than semantics.

The Ada standard does have a useful innovation from a pragmatic point of view
in having sections called Implementation Advice, which are allowed to say
things that not strictly meaningful from a formal semantic point of view, but
which are useful pragmatically (of course these sections are not normative).

So referring to the ISO C++ standard is simply not relevant from a formal
point of view. Indeed the question on what code to generate should be
addressed from a pragmatic point of view, and of course this viewpoint must
include what C++ programmers expect historically (note that this is different
from what Gaby or any one person might believe about such expectations!)

Gaby's absolute position that any compiler that does not always inline 
everything is incorrect (presumably his intelligent C++ programmer would
expect inifinite compile time or a blowup in the case of a recursion???)
is to me highly contentious. I assume Gaby would also describe the (to me
very reasonable) strategy of the Intel compiler as incorrect.

I really think the only useful continuation of this discussoin is to have
some actual data, based on the absolutely-on-target suggestions made 
earlier this morning. 

I for one am signing off this thread till i see some data :-)

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 18:22 Robert Dewar
  2003-08-04 18:29 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 18:22 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aoliva, bernds, gcc, jbuck, rguenth, s.bosscher

> It is an "as-if" rule only because that is the way it is described in
> standardese.   In the C++ community, we do care about history and
> documented behaviour.   You won't change that, just because you want
> C++ inline to have a less language specific meaning.

It's the only possibloe description in standardese here.

The point is that appealing to the ISO standard (as you did a few msgs ago)
is not particularly helpful, since the standard really has nothing to say.
This is a matter that must be decided, as with any code generation issue,
on the basis of what is pragnmatically best.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 18:21 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 18:21 UTC (permalink / raw)
  To: gdr, jbuck; +Cc: aoliva, bernds, dewar, gcc, rguenth, s.bosscher

> I won't insist on the "as-if" rule: the longstanding promise in the GCC
> manual that "an inlined function is as fast as a macro" will do for me
> as a spec.  If you can achieve that without actually doing the inlining,
> go for it.

That's a problematic spec, it is not at all clear that you cam make
this guarantee. In our experience, you often get some junk from the
inlining process in gcc that is eliminated if you do macro expansion.

In fact we recently implemented front end inlining in GNAT (the -gnatN
switch) which gives something closer to C macro behavior, and we often
find that we get better code.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 18:08 Robert Dewar
  2003-08-04 18:17 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 18:08 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aoliva, bernds, gcc, jbuck, rguenth, s.bosscher

> I do insist on the fact that it is not just an "as-if" rule.

You can insist all you like, but every rule in a standard of this kind
is an as-if rule. It would require a formal apparatus and set of definitions
far beyond what the C++ standard attempts for it to be otherwise.

The standard describes the meaning of a program in formal semantic terms
by giving a formal semantics to C++ programs. It does not specify what
code should be generated, only what that code must mean when it is executed.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 17:48 Robert Dewar
  2003-08-04 17:57 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 17:48 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aoliva, bernds, gcc, jbuck, rguenth, s.bosscher

> Defining a member function within a class declaration has exactly the
> same meaning.  And that has been documented in every manual since 1981.

Yes, we all agree that the as-if standard meaning is the same, that is
never in dispute. A compiler that ignores all inlining is perfectly
well in conformance, and that might make sense on a machine which favors
this approach, e.g. the transputer where a call is much faster than a jump.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 17:43 Robert Dewar
  2003-08-04 17:53 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 17:43 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aoliva, bernds, gcc, jbuck, rguenth, s.bosscher

> You should not underestimate C++ programmers ability to make considerations.

I think you make the mistake of overestimating!

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 17:33 Robert Dewar
  2003-08-04 17:36 ` Joe Buck
                   ` (2 more replies)
  0 siblings, 3 replies; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 17:33 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: aoliva, bernds, gcc, gdr, rguenth, s.bosscher

> Sigh.  In C++, the programmer has already done the needed analysis, and
> has attached the keyword "inline" or defined the function in the class
> body.  Certainly, with -O3 the kind of analysis you describe would be
> appropriate, though possibly expensive.


This claim is made repeatedly, but without any evidence. I think a lot of
C++ programmers decide whether to put thins in the class body on the basis
of other considerations than whether to inline -- a consideration that is
pretty hard to do for the great majority of C++ (or Ada for that matter)
programmers who have not the slightest awareness of machine language or
its implications.

I do agree that if the programmer has attached the keyword "inline" then
that shows at least some thought, though whether it is well informed or
not can often be an issue.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-08-04 17:03 Robert Dewar
  2003-08-04 17:23 ` Joe Buck
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-08-04 17:03 UTC (permalink / raw)
  To: aoliva, bernds; +Cc: gcc, gdr, rguenth, s.bosscher

> (*) Reasons why I'd accept inline not being honoured:
>   - the function would become too big for the machine (or any other reason
>     why the code wouldn't work)
>   - recursive functions
>   - alloca or some other feature that causes stack size explosions
>     is used.  We should warn - this is a case for always_inline
>     (Likewise for any dodgy GNU extensions I'm forgetting right now)
>   - the compiler would explode due to lack of memory - we should make an
>     effort this doesn't become a frequent reason
>   - the programmer used -Os (and we can prove the code would get bigger)


I would add

- doing the inlining as requested increases both the space and execution time
of the program, the former very substantially.

Of course, the question is

1. Does this happen often in practice (certainly as I Have noted before we
often see that -O3 slows things down compared to -O2: speed = space in the
land of icaches).

2. Can the compiler really usefully tell this is the case, or does it make
so many mistakes trying to be helpful in this way that it actually hurts.

With regard to the "collapsing code" issue, clearly the best strategy is
to do parameter substitution and collapse the code *before* deciding whether
to inline it. Of course this is not so easy to do.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-31 12:05 Robert Dewar
  2003-07-31 13:00 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-31 12:05 UTC (permalink / raw)
  To: gdr, rguenth; +Cc: gcc, martin

> Please do be careful about the fact that I'm not proposing that g++
> should not inline a function not marked inline.

your sentences have impossibly many negatives. I can't even make an
attempt to understand what a sentence like the above means or is
meant to mean.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-31 11:50 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-31 11:50 UTC (permalink / raw)
  To: gdr, rguenth; +Cc: gcc, martin

> Martin is right that there is a lack of being able to express what one
> means. And which this lack you cannot be sure if the programmer with
> "inline" meant inline or maybe inline. You cannot even be sure if he meant
> do not inline if he omitted inline. It doesnt help that _you_ mean inline
> if you write it.

Note that in GNAT we have added pragma Inline_Always which says you absolutely
must inline, and it is an error if you cannot do so. pragma Inline means
that it is desirable to inline if it makes sense and is technically possible,
and not saying anything leaves the compiler free to inline if it thinks this
is a good idea (with most Ada code, this does not help much).

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-31 10:54 Richard Guenther
  2003-07-31 11:08 ` Gabriel Dos Reis
  2003-07-31 11:31 ` Rob Taylor
  0 siblings, 2 replies; 211+ messages in thread
From: Richard Guenther @ 2003-07-31 10:54 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Martin Reinecke, gcc

 Martin Reinecke <martin@mpa-garching.mpg.de> writes:

 | Gabriel Dos Reis wrote:
 |
 | > It is tempting to make the analogy with "register", but you have to
 | > acknowledge the fact that inlining strategy is not at the same level
 | > of sophistication as register allocation.  Therefore, any answer you
 | > get for the question above is irrelevant to whether you should decide
 | > whether your programmed inliner always knows better than the
 | > programmer.   Until then, please listen to the programmer.  Don't
 | > transmute "inline".
 |
 | Yes, you should listen to the programmer. But unless the programmer
 | has a chance to express his intent _exactly_, there is no way to find
 | out what he really meant. And he cannot do so in the discussed situation.

  How to be sure there would not be another quibble about whether he
  really wanted to express exactly his intent?  something like

     [P] inline
     [C] do you inline mean inline?
     [P] Yes
     [C] Are you sure you really mean inline?
     [P] Yes
     [C] Did you mean the last answer?
     [P] ...

Ah, come on, stop whining and making this list look like a Kindergarten.

  That way lies infinite recursion with no way of getting started,
  i.e. no productive work.  We don't need complication to get decent
  results.  KCC didn't need a hierarchy of intent of inline before
  producing much better code.  When we will reach the level where the
  compiler trusts the programmer, we could start worrying about what is
  next.  Like they say here, Paris is built in one day.
  First, start trusting the programmer; when you're good at that job, you
  would be able to handle subtile  distinctions.

Martin is right that there is a lack of being able to express what one
means. And which this lack you cannot be sure if the programmer with
"inline" meant inline or maybe inline. You cannot even be sure if he meant
do not inline if he omitted inline. It doesnt help that _you_ mean inline
if you write it.

So face it - gcc _has_ to map both "inline" and not "inline" to
maybe-inline. There are (non portable) ways to specify do not inline
(attribute noinline) and do always inline (attribute always_inline).
The way out of this discussion is to make gcc's heuristics better, not to
force it into any static notion about "inline" or not "inline".

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-31 10:19 Martin Reinecke
  2003-07-31 10:38 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Martin Reinecke @ 2003-07-31 10:19 UTC (permalink / raw)
  To: gdr; +Cc: gcc

Gabriel Dos Reis wrote:

> It is tempting to make the analogy with "register", but you have to
> acknowledge the fact that inlining strategy is not at the same level
> of sophistication as register allocation.  Therefore, any answer you
> get for the question above is irrelevant to whether you should decide
> whether your programmed inliner always knows better than the
> programmer.   Until then, please listen to the programmer.  Don't
> transmute "inline".

Yes, you should listen to the programmer. But unless the programmer
has a chance to express his intent _exactly_, there is no way to find
out what he really meant. And he cannot do so in the discussed situation.

When writing a member function, the programmer has to adress the question
"Should this function be inlined?", and you agreed that there are three
valid answers: "yes", "no", and "it depends; let the compiler decide".

But current g++ only allows two ansers:
  "yes" (i.e. make the function inline), and
  "no"  (i.e. define the function in some .cc file)

If the programmer can only choose two of three alternatives, it is not
possible to determine whether he didn't actually want to say "it depends".
If he says "no", he most certainly means "no", because then g++ has not much
chance of inlining the function if it's not defined in the current translation
unit.
But if he says "yes", how can you be sure he really meant "yes" and not
"it depends"? The language practically forces him to lie, and therefore
you can't trust him blindly.

My opinion is that if the programmer says "yes", the only thing you can be sure
about is that he didn't mean "no", but that's not enough to determine whether
g++ should unconditionally inline or not.

Of course, this argument breaks down if there is some way to say "it depends".
I don't know of any. If there is, please show me!

Martin


^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 22:49 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 22:49 UTC (permalink / raw)
  To: Richard.Earnshaw, gdr; +Cc: aoliva, coyote, gcc, rguenth

> | If the original C specification had said that "register" meant "register" 
> | unconditionally, then the situation wouldn't have been any different -- 
> | the compiler would have to obey the user, even if the compiler could do 
> | better.

That would be very hard for a standard to say, given that standards describe
as-if semantics and are not in the business of telling you what sequences
of instructions may or may not be used to implement these semantics.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 22:28 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 22:28 UTC (permalink / raw)
  To: gdr, jbuck; +Cc: aoliva, gcc, rguenth, s.bosscher

> Gaby, the scenario Alex describes is COMMON.  Constant propagation
> frequently causes lots of code to be thrown away, and any automatic
> inlining decision that doesn't take this effect into account is broken.


Note thjat this is even more the case in Ada, where the normal way of doing
conditional compilation is by the use of constant booleans (there is no
macro preprocessing defined in Ada).

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 21:49 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 21:49 UTC (permalink / raw)
  To: dewar, gdr; +Cc: Richard.Earnshaw, aoliva, coyote, gcc, kgardas, rguenth

> | In fact, the liberal use of inline tends to *prevent* premature optimization
> 
> That might be true for Ada, I'm doubteful it translates to C++ word-for-word.


I really don't see a difference, other than that Ada programmers have been
used to using inline liberally for twenty years :-)

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 16:36 Robert Dewar
  2003-07-30 16:49 ` Gabriel Dos Reis
                   ` (2 more replies)
  0 siblings, 3 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 16:36 UTC (permalink / raw)
  To: Richard.Earnshaw, coyote; +Cc: aoliva, gcc, gdr, kgardas, rguenth

> Inlining is an optimization; in all but the most obvious and trivial
> cases (e.g., simple assignments), it should be performed by a programmer 
> *after* profiling, and not before. "Premature optimization is the root 
> of all evil", according to Hoare and Knuth; in code reviews, I tend to 
> grumble about people who insist on inlining and using cute tricks, when 
> they haven't done any analysis as to how their choices affect code 
> quality and performance.

I strongly disagree with this. Profiling is simply not an effective way of
identifying functions that benefit from inlining. Profiling is a very crude
tool that is helpful in eliminating hot spots but that's all.

The reason for not taking this view of inlining is that unless you get in the
mindset that inlining will be done routinely by the compiler (at your direction)
then you will tend to shy away from the use of trivial abstraction functions
because you will worry about efficiency.

The quote about premature optimization here is wrong I think. What Knuth
was talking about (go back and look at what he really said, not just the
sound bite) was distorting the code prematurely and unnecessarily in the
interests of optimization.

In fact, the liberal use of inline tends to *prevent* premature optimization
(of the kind I mentioned above, avoidance of abstraction), and certainly does
not distort the code.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 16:08 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 16:08 UTC (permalink / raw)
  To: coyote, martin; +Cc: gcc

note that one of the most important reasons for inlining *on some architectures*
is to deal with cache thrashing when a function is called within a loop. This of
course is something that is out of reach of analysis by the programmer or
the compiler, but it can often mean that inlining helps even though to a
simple minded analysis it does not seem to.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 16:06 Richard Guenther
  2003-07-30 16:37 ` Martin Reinecke
  0 siblings, 1 reply; 211+ messages in thread
From: Richard Guenther @ 2003-07-30 16:06 UTC (permalink / raw)
  To: Martin Reinecke; +Cc: gcc


> Something seems fundamentally wrong here, and I think it is C++'s property
> to automatically put the "inline" tag on all functions defined in a class
> body, even if they don't have the "inline" keyword. But I see no chance
> to get this changed.

-fno-default-inline is your friend.

Richard.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:25 Robert Dewar
  2003-07-30 14:49 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:25 UTC (permalink / raw)
  To: martin; +Cc: gcc

> If we assume for now that intermodule optimization is not done (and that's
> the current situation),

For the record, intermodule inlining is considered a critically important
optimziation in the Ada world.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:21 Robert Dewar
  2003-07-30 14:44 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:21 UTC (permalink / raw)
  To: Richard.Earnshaw, gdr; +Cc: aoliva, gcc, kgardas, rguenth

> 
> Suggesting that a programmer must only use inline when they are convinced 
> that better code will always result (ie that using inline when it may only 
> sometimes produce better code is "dangerous") sounds even more like 
> spreading FUD to me.  You're going to get people saying "Never use inline, 
> it can make your code worse".

I think one reason that the treatment of inline has been much more successful
in Ada than in C++ (at least in the gcc environment) is precisely that Ada
programmers are very used to using inline extensively, and assuming that
the compiler will pay significant attention to their input. In fact we find
that -O3 often damages performance in Ada, since the programmer has done
a pretty good job of selecting what needs inlining.

So anything that discourages the use of the inline directive is unfortunate

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:18 Robert Dewar
  2003-07-30 14:31 ` Richard Earnshaw
  2003-07-30 14:36 ` Richard Guenther
  0 siblings, 2 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:18 UTC (permalink / raw)
  To: gdr, rguenth; +Cc: Richard.Earnshaw, aoliva, gcc, kgardas

> The only sane possible semantics I see are:
> 
> 1. inline declared functions are inlined always if technically possible
> 2. the inline keyword has no effect
> 3. inline is handled in an implementation defined manner (as stated in the
>    standard), maybe by adjusting the set of functions considered for inlining,
>    as gcc does.

None of these three semantics make sense to me. The only one that makes sense
is the one I gave before.

Speed up calls to this function, by doing optimizations (most obviously by
inlining the call) that speed up execution, even if it results in extra space
for generated code.

(the comment here is suggestive, not normative, in standards terms, but we
know perfectly well what it means as implementors).

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:11 Robert Dewar
  2003-07-30 14:29 ` Richard Guenther
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:11 UTC (permalink / raw)
  To: dewar, rguenth; +Cc: aoliva, gcc, gdr, s.bosscher

> Also portability is not only about different targets, but also about
> different compilers! Declaring something inline for one compiler doesnt
> necessary have the same desired semantics for inline for another one.

good point ...

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:05 Robert Dewar
  2003-07-30 14:14 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:05 UTC (permalink / raw)
  To: Richard.Earnshaw, gdr; +Cc: aoliva, gcc, kgardas, rguenth

> He can profile.

Profiling is of limited use here. It may find hotspots, but the issue of
how to treat large numbers of small functions that should be inlined cannot
be addressed by profiling, since you may have situations where you have many
such functions and no one function shows up as a hot spot. For an example
look at the set of pragma Inlines for the Ada units sinfo.ads and einfo.ads.

Basically in Ada, we regard pragma Inline as meaning: this function is pretty
small and I call it quite often and I would like the calls to be efficient.
Feel free to optimize in a way that improves time performance even if more
space is generated. I am guessing that it would make sense to inline calls
(I know the standard doesn't formally define this, but you know what I mean).

What we do in practice is to mark the function as inlinable, and use a higher
than usual threshhold for deciding to inline it, which seems to work pretty
well.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 14:04 Robert Dewar
  2003-07-30 14:10 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 14:04 UTC (permalink / raw)
  To: gdr, martin; +Cc: gcc

> But this implies an "inline" directive
> which would more or less _force_ the compiler to always inline the code.

Yes, but there is nothing in the standard or in common sense that requires
you to treat this implicit inline the same way you would treat other inlines,
since the compiler has plenty of freedom in its handling of inline.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 13:52 Robert Dewar
  2003-07-30 14:02 ` Richard Guenther
  0 siblings, 1 reply; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 13:52 UTC (permalink / raw)
  To: aoliva, gdr; +Cc: gcc, rguenth, s.bosscher

> 
> Therefore, inline the way you describe it, is useful only for
> functions that are *always* profitable to inline.  Any function that
> might or might not be profitable to inline should not be declared
> inline, and the compiler would never inline it.

And of course this approach is impractical, since whether inlining helps
in a specific case is target dependent.

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 13:25 Robert Dewar
  0 siblings, 0 replies; 211+ messages in thread
From: Robert Dewar @ 2003-07-30 13:25 UTC (permalink / raw)
  To: aoliva, gdr; +Cc: gcc, rguenth, s.bosscher

> I haven't chosen anything, I just quoted the C Standard.  It clearly
> calls for compiler's effort towards fast execution over inline
> substitution.  Compared with that, what you claim to be C++'s intended
> meaning (inline even if the resulting code is worse) seems as a very
> misguided idea.

In fact in a language standard, you really can't define the notion of
"inlining" as such, since the standard is not in the business of
describing details about the generated code. So the formal meaning of
inline is most certainly "speed up execution of this function if possible
at the expense of extra space". Yes, notes in the standard and the name
chosen for this hint is strongly evocative, but not normative :-)

^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 13:13 Martin Reinecke
  2003-07-30 13:30 ` Gabriel Dos Reis
  2003-07-30 15:56 ` Scott Robert Ladd
  0 siblings, 2 replies; 211+ messages in thread
From: Martin Reinecke @ 2003-07-30 13:13 UTC (permalink / raw)
  To: gcc

Gabriel Dos Reis wrote:

 > Do trust the programmer.

This is certainly a valid point of view, but I think that C++, as it exists
at the moment, just _doesn't give_ the programmer the possibility of specifying
what exactly (s)he wants.

I think it is not generally possible for a programmer to decide whether a function
should be inlined or not (see below).

The only exceptions are functions like std::string::end()
that probably even *save space* when they are inlined.

But imagine, for example, a function foo() with about 50 lines of code, somewhere in a C++
library. If this function is going to be used in the following context

amax = 100000000;
[...]
for (int a=0; a<=amax; ++a)
   foo();

then it should most definitely be inlined, because not much space is wasted, and the
calling overhead is gone, which pays off if amax is large.
The problem is that gcc usually cannot determine how large amax is going to be, so it
doesn't know what the right solution is, no matter how good the inlining heuristics are.

On the other hand, if foo() is called from many different places but only once, like this:

[...]
foo();
[...]
foo();
and so on,

then inlining probably doesn't make much sense.

In other words: the author of foo() _cannot_ decide whether foo() should be inlined or not,
because he doesn't know to what kind of uses it will be put by other people (unless he is the
only user of foo(), of course). Whatever the author of foo() specifies (inline or not),
he's going to get it wrong in some cases.

In most cases, the user who calls foo() knows much better if foo() should be inlined.
Wouldn't it therefore be much more sensible to give the compiler an inlining hint at the place
where foo() is called, e.g.

for (int a=0; a<=amax; ++a)
#pragma inline
   foo();

or

#pragma noinline
{
[...]
foo();
[...]
foo();
}

Of course it would be much nicer if C++ supported this natively ...

Please ignore me if I didn't make any sense.

Cheers,
   Martin


^ permalink raw reply	[flat|nested] 211+ messages in thread
* Re: std::pow implementation
@ 2003-07-30 12:55 Stephan T. Lavavej
  2003-07-30 12:59 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Stephan T. Lavavej @ 2003-07-30 12:55 UTC (permalink / raw)
  To: gcc

It should be noted that the only way to make a library that is entirely
implemented in its header files is to declare *everything* inline.  Thus,
sometimes inline is used without the programmer intending it to mean
anything about performance one way or the other.

Stephan T. Lavavej
http://stl.caltech.edu



^ permalink raw reply	[flat|nested] 211+ messages in thread
* std::pow implementation
@ 2003-07-29 11:57 Richard Guenther
  2003-07-29 12:10 ` Gabriel Dos Reis
  0 siblings, 1 reply; 211+ messages in thread
From: Richard Guenther @ 2003-07-29 11:57 UTC (permalink / raw)
  To: gcc

Hi!

The current std::pow implementation causes very weak code to be emitted
for the overloads pow(T, int) for both gcc 3.3 and gcc 3.4. These powers
are computed going through the std::__power_helper() function template
which ends up emitting a call to std::__cmath_power() always. For
reference:

  template<typename _Tp>
    inline _Tp
    __pow_helper(_Tp __x, int __n)
    {
      return __n < 0
        ? _Tp(1)/__cmath_power(__x, -__n)
        : __cmath_power(__x, __n);
    }

If we change this to read like

  template<typename _Tp>
    inline _Tp __attribute__((always_inline))
    __power_helper(_Tp __x, int __n)
    {
      if (__builtin_constant_p(__n)) {
          switch (__n) {
          case 0:
                return 1;
          case 1:
                return __x;
          case 2:
                return __x*__x;
#if ! __OPTIMIZE_SIZE__
          case 3:
                return __x*__x*__x;
          case 4: {
                _Tp __y = __x*__x;
                return __y*__y;
          }
          /* etc. */
#endif
          }
      }
      return __n < 0
        ? _Tp(1)/std::__cmath_power(__x, -__n)
        : std::__cmath_power(__x, __n);
    }

we will win a lot.

Can such be done for 3.4 and possibly 3.3, too?

Thanks,

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

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

end of thread, other threads:[~2003-08-04 22:23 UTC | newest]

Thread overview: 211+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-30 13:56 std::pow implementation Robert Dewar
2003-07-30 14:04 ` Richard Guenther
2003-07-30 14:08 ` Gabriel Dos Reis
2003-07-31  2:10   ` Alexandre Oliva
2003-07-31  2:46     ` Gabriel Dos Reis
2003-07-31  6:49       ` Alexandre Oliva
2003-07-31  7:58         ` Gabriel Dos Reis
2003-07-31  8:14         ` Gabriel Dos Reis
  -- strict thread matches above, loose matches on Subject: below --
2003-08-04 22:57 Robert Dewar
2003-08-04 18:42 Robert Dewar
2003-08-04 18:46 ` Gabriel Dos Reis
2003-08-04 18:22 Robert Dewar
2003-08-04 18:29 ` Gabriel Dos Reis
2003-08-04 18:21 Robert Dewar
2003-08-04 18:08 Robert Dewar
2003-08-04 18:17 ` Gabriel Dos Reis
2003-08-04 17:48 Robert Dewar
2003-08-04 17:57 ` Gabriel Dos Reis
2003-08-04 18:14   ` Joe Buck
2003-08-04 17:43 Robert Dewar
2003-08-04 17:53 ` Gabriel Dos Reis
2003-08-04 17:33 Robert Dewar
2003-08-04 17:36 ` Joe Buck
2003-08-04 17:38 ` Gabriel Dos Reis
2003-08-04 22:28 ` Mark Hahn
2003-08-04 22:49   ` tm_gccmail
2003-08-04 17:03 Robert Dewar
2003-08-04 17:23 ` Joe Buck
2003-08-04 18:05   ` Richard Earnshaw
2003-08-04 18:15     ` Gabriel Dos Reis
2003-08-04 19:00     ` Bernd Schmidt
2003-08-04 19:12       ` Richard Guenther
2003-07-31 12:05 Robert Dewar
2003-07-31 13:00 ` Gabriel Dos Reis
2003-07-31 11:50 Robert Dewar
2003-07-31 10:54 Richard Guenther
2003-07-31 11:08 ` Gabriel Dos Reis
2003-07-31 11:31 ` Rob Taylor
2003-07-31 14:13   ` Scott Robert Ladd
2003-07-31 14:44     ` Gabriel Dos Reis
2003-07-31 17:24     ` Steven Bosscher
2003-07-31 18:45       ` Scott Robert Ladd
2003-07-31 10:19 Martin Reinecke
2003-07-31 10:38 ` Gabriel Dos Reis
2003-07-30 22:49 Robert Dewar
2003-07-30 22:28 Robert Dewar
2003-07-30 21:49 Robert Dewar
2003-07-30 16:36 Robert Dewar
2003-07-30 16:49 ` Gabriel Dos Reis
2003-07-30 16:55 ` Scott Robert Ladd
2003-07-30 19:21 ` Felix Lee
2003-07-30 16:08 Robert Dewar
2003-07-30 16:06 Richard Guenther
2003-07-30 16:37 ` Martin Reinecke
2003-07-30 14:25 Robert Dewar
2003-07-30 14:49 ` Gabriel Dos Reis
2003-07-30 14:21 Robert Dewar
2003-07-30 14:44 ` Gabriel Dos Reis
2003-07-30 14:51   ` Paolo Carlini
2003-07-30 16:59     ` Joe Buck
2003-07-30 17:35       ` Richard Earnshaw
2003-07-30 14:18 Robert Dewar
2003-07-30 14:31 ` Richard Earnshaw
2003-07-30 14:36 ` Richard Guenther
2003-07-30 14:11 Robert Dewar
2003-07-30 14:29 ` Richard Guenther
2003-07-30 14:05 Robert Dewar
2003-07-30 14:14 ` Gabriel Dos Reis
2003-07-30 14:04 Robert Dewar
2003-07-30 14:10 ` Gabriel Dos Reis
2003-07-30 13:52 Robert Dewar
2003-07-30 14:02 ` Richard Guenther
2003-07-30 13:25 Robert Dewar
2003-07-30 13:13 Martin Reinecke
2003-07-30 13:30 ` Gabriel Dos Reis
2003-07-30 13:40   ` Martin Reinecke
2003-07-30 13:46     ` Andrew Pinski
2003-07-30 13:47       ` Steven Bosscher
2003-07-30 14:32         ` Martin Reinecke
2003-07-30 13:53     ` Gabriel Dos Reis
2003-07-30 14:14       ` Martin Reinecke
2003-07-30 14:33         ` Gabriel Dos Reis
2003-07-30 15:27           ` Martin Reinecke
2003-07-30 15:42             ` Gabriel Dos Reis
2003-07-30 17:38               ` Martin Reinecke
2003-08-04 12:55           ` Theodore Papadopoulo
2003-08-04 13:11             ` Gabriel Dos Reis
2003-08-04 14:32               ` Theodore Papadopoulo
2003-08-04 14:50                 ` Gabriel Dos Reis
2003-08-04 14:58                   ` Daniel Berlin
2003-07-30 15:56 ` Scott Robert Ladd
2003-07-30 16:16   ` Steven Bosscher
2003-07-30 16:47     ` Scott Robert Ladd
2003-07-30 12:55 Stephan T. Lavavej
2003-07-30 12:59 ` Gabriel Dos Reis
2003-07-29 11:57 Richard Guenther
2003-07-29 12:10 ` Gabriel Dos Reis
2003-07-29 12:10   ` Richard Guenther
2003-07-29 12:14     ` Gabriel Dos Reis
2003-07-29 12:25       ` Richard Guenther
2003-07-29 12:38         ` Gabriel Dos Reis
2003-07-29 12:44           ` Richard Guenther
2003-07-29 12:49             ` Gabriel Dos Reis
2003-07-30  5:18               ` Alexandre Oliva
2003-07-30  5:26                 ` Gabriel Dos Reis
2003-07-30  6:57                   ` Alexandre Oliva
2003-07-30 10:11                     ` Gabriel Dos Reis
2003-07-30 11:58                       ` Richard Earnshaw
2003-07-30 12:11                         ` Gabriel Dos Reis
2003-07-30 12:13                           ` Steven Bosscher
2003-07-30 12:23                             ` Gabriel Dos Reis
2003-07-30 12:31                               ` Steven Bosscher
2003-07-30 12:47                                 ` Gabriel Dos Reis
2003-07-30 13:06                                   ` Steven Bosscher
2003-07-30 13:22                                     ` Gabriel Dos Reis
2003-07-30 12:42                               ` Richard Guenther
2003-07-30 12:46                                 ` Gabriel Dos Reis
2003-07-30 13:01                                   ` Richard Guenther
2003-07-30 13:26                                     ` Steven Bosscher
2003-07-30 13:38                                       ` Richard Guenther
2003-07-30 13:49                                         ` Gabriel Dos Reis
2003-07-30 13:19                           ` Karel Gardas
2003-07-30 13:24                             ` Gabriel Dos Reis
2003-07-30 13:41                             ` Richard Earnshaw
2003-07-30 13:51                               ` Gabriel Dos Reis
2003-07-30 13:51                                 ` Richard Earnshaw
2003-07-30 13:59                                   ` Gabriel Dos Reis
2003-07-30 14:08                                     ` Richard Guenther
2003-07-30 14:19                                       ` Richard Guenther
2003-07-30 14:24                                       ` Gabriel Dos Reis
2003-07-30 14:48                                         ` Richard Guenther
2003-07-30 14:55                                           ` Gabriel Dos Reis
2003-07-30 15:29                                             ` Richard Guenther
2003-07-31  0:30                                       ` Richard B. Kreckel
2003-07-30 14:11                                     ` Richard Earnshaw
2003-07-30 14:26                                       ` Gabriel Dos Reis
2003-07-30 16:25                                   ` Scott Robert Ladd
2003-07-30 13:59                                 ` Richard Guenther
2003-07-30 14:01                                   ` Gabriel Dos Reis
2003-07-30 15:45                         ` Scott Robert Ladd
2003-07-30 16:50                           ` Richard Earnshaw
2003-07-30 16:57                             ` Gabriel Dos Reis
2003-07-30 17:42                               ` Richard Earnshaw
2003-07-30 18:06                                 ` Gabriel Dos Reis
2003-07-30 17:02                             ` Scott Robert Ladd
2003-07-30 19:31                             ` tm_gccmail
2003-07-30 17:32                         ` Joe Buck
2003-07-29 12:53           ` Steven Bosscher
2003-07-29 12:53             ` Gabriel Dos Reis
2003-07-29 12:58               ` Richard Guenther
2003-07-29 12:59                 ` Steven Bosscher
2003-07-29 13:05                   ` Paolo Carlini
2003-07-29 13:22                     ` Richard Guenther
2003-07-29 13:36                       ` Steven Bosscher
2003-07-29 14:14                         ` Richard Guenther
2003-07-29 14:22                         ` Richard Guenther
2003-07-29 13:00                 ` Andrew Pinski
2003-07-29 13:28                   ` Richard Guenther
2003-07-29 13:59                     ` Andrew Pinski
2003-07-29 14:17                       ` Gabriel Dos Reis
2003-07-29 13:14                 ` Gabriel Dos Reis
2003-07-29 13:14               ` Steven Bosscher
2003-07-29 14:08                 ` Gabriel Dos Reis
2003-07-29 14:24                   ` Steven Bosscher
2003-07-29 14:24                     ` Gabriel Dos Reis
2003-07-29 14:31                     ` Gabriel Dos Reis
2003-07-29 14:40                       ` Steven Bosscher
2003-07-29 15:11                         ` Gabriel Dos Reis
2003-07-29 15:37                           ` Michael Matz
2003-07-29 15:59                             ` Gabriel Dos Reis
2003-07-29 15:59                               ` Michael Matz
2003-07-29 16:05                                 ` Gabriel Dos Reis
2003-07-29 16:20                                   ` Rob Taylor
2003-07-29 14:36                     ` Gabriel Dos Reis
2003-07-29 15:24                       ` Richard Guenther
2003-07-29 16:30                         ` Gabriel Dos Reis
2003-07-29 18:35                           ` Richard Guenther
2003-07-29 14:51                     ` Gabriel Dos Reis
2003-07-29 15:33                       ` Steven Bosscher
2003-07-30  5:24                       ` Alexandre Oliva
2003-07-30  5:33                         ` Gabriel Dos Reis
2003-07-30  6:38                           ` Alexandre Oliva
2003-07-30 10:32                             ` Gabriel Dos Reis
2003-07-30 10:33                               ` Alexandre Oliva
2003-07-30 10:46                                 ` Gabriel Dos Reis
2003-07-30 11:57                                   ` Alexandre Oliva
2003-07-30 12:20                                     ` Gabriel Dos Reis
2003-07-30 15:50                                     ` Scott Robert Ladd
2003-07-30 15:53                                       ` Steven Bosscher
2003-07-30 15:53                                         ` Richard Guenther
2003-07-30 16:01                                         ` Gabriel Dos Reis
2003-07-30 16:09                                           ` Steven Bosscher
2003-07-30 16:39                                             ` Gabriel Dos Reis
2003-07-30 16:17                                           ` Richard Guenther
2003-07-30 16:24                                             ` Steven Bosscher
2003-08-04 16:55                                     ` Bernd Schmidt
2003-08-04 17:08                                       ` Alexandre Oliva
2003-07-30 10:37                               ` Steven Bosscher
2003-07-30 11:31                                 ` Gabriel Dos Reis
2003-07-30 15:44                               ` Scott Robert Ladd
2003-07-30 17:10                                 ` Joe Buck
2003-07-30 17:32                                   ` Richard Guenther
2003-07-30 18:22                                     ` Daniel Berlin
2003-07-30 19:08                                       ` Richard Guenther
2003-07-30 19:12                                         ` Daniel Berlin
2003-07-30 17:06                           ` Joe Buck
2003-07-30 17:26                             ` Gabriel Dos Reis
2003-07-29 19:58   ` Neil Booth
2003-07-29 20:14     ` Gabriel Dos Reis
2003-07-29 20:33       ` Richard Guenther
2003-07-29 20:49         ` Gabriel Dos Reis

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