comp.lang.ada
 help / color / mirror / Atom feed
* Eliminating copying in Gnat
@ 2003-01-20  4:12 Victor Porton
  2003-01-20 14:57 ` Damien CARBONNE
                   ` (5 more replies)
  0 siblings, 6 replies; 16+ messages in thread
From: Victor Porton @ 2003-01-20  4:12 UTC (permalink / raw)


Is it possible to cause Gnat to not do unnecessary copying
when calling this fynction?

function Pass(X: T) return T is
  pragma Inline(Pass);
begin
  return X;
end;

If T is a controlled type, it calls Adjust 3(!) times even
if I compile with optimization ("-O3 -gnatN").

I even don't know how to check (well, without reading assembler
output) whether copying would be done in the case of
non-controlled T.

It is in Gnat-3.14p. May be it was fixed in Gnat-3.2/3.15p?



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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
@ 2003-01-20 14:57 ` Damien CARBONNE
  2003-01-20 15:03   ` Lutz Donnerhacke
  2003-01-20 18:34   ` Martin Krischik
  2003-01-20 15:08 ` Stephen Leake
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 16+ messages in thread
From: Damien CARBONNE @ 2003-01-20 14:57 UTC (permalink / raw)


Victor Porton wrote:

> Is it possible to cause Gnat to not do unnecessary copying
> when calling this fynction?
>
> function Pass(X: T) return T is
>   pragma Inline(Pass);
> begin
>   return X;
> end;

I'm not completely sure about it, but I would not put the "pragma
Inline" at this place, but rather in the spec file (.ads).

package XXX is
   function Pass(X:T) return T;
   pragma Inline(Pass);
end XXX;




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

* Re: Eliminating copying in Gnat
  2003-01-20 14:57 ` Damien CARBONNE
@ 2003-01-20 15:03   ` Lutz Donnerhacke
  2003-01-20 18:34   ` Martin Krischik
  1 sibling, 0 replies; 16+ messages in thread
From: Lutz Donnerhacke @ 2003-01-20 15:03 UTC (permalink / raw)


* Damien CARBONNE wrote:
> package XXX is
>    function Pass(X:T) return T;
>    pragma Inline(Pass);
> end XXX;

Inline_Always does work even over package boundaries. (At the cost of
recompiling the body every time.)



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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
  2003-01-20 14:57 ` Damien CARBONNE
@ 2003-01-20 15:08 ` Stephen Leake
  2003-01-20 21:27 ` Victor Porton
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 16+ messages in thread
From: Stephen Leake @ 2003-01-20 15:08 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> Is it possible to cause Gnat to not do unnecessary copying
> when calling this fynction?
> 
> function Pass(X: T) return T is
>   pragma Inline(Pass);
> begin
>   return X;
> end;

Try using a separate specification for Pass, and applying pragma
Inline there:

function Pass(X: T) return T;
pragma Inline(Pass);

function Pass(X: T) return T is
begin
  return X;
end;

I'm not clear if this is the problem.

> If T is a controlled type, it calls Adjust 3(!) times even if I
> compile with optimization ("-O3 -gnatN").

Post complete code that demonstrates this; maybe I'll have other
suggestions. 

> It is in Gnat-3.14p. May be it was fixed in Gnat-3.2/3.15p?

I can test with Gnat 3.15p that if I have complete code.

-- 
-- Stephe



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

* Re: Eliminating copying in Gnat
  2003-01-20 14:57 ` Damien CARBONNE
  2003-01-20 15:03   ` Lutz Donnerhacke
@ 2003-01-20 18:34   ` Martin Krischik
  1 sibling, 0 replies; 16+ messages in thread
From: Martin Krischik @ 2003-01-20 18:34 UTC (permalink / raw)


On Mon, 20 Jan 2003 15:57:08 +0100, Damien CARBONNE wrote:

> Victor Porton wrote:
 
>> Is it possible to cause Gnat to not do unnecessary copying
>> when calling this fynction?
>>
>> function Pass(X: T) return T is
>>   pragma Inline(Pass);
>> begin
>>   return X;
>> end;

Funny as you say, I did the very same thing today and the compiler actualy
told me in no uncertain terms that it did not like it. But then I am a
masochist with all warnings swiched on.
 
> I'm not completely sure about it, but I would not put the "pragma
> Inline" at this place, but rather in the spec file (.ads).
> 
> package XXX is
>    function Pass(X:T) return T;
>    pragma Inline(Pass);
> end XXX;

I think you are right. The compiler liked that Version much more.

With Regards

Martin.

PS: The Compiler used was GNAT for OS/2.

-- 
Martin Krischik
mailto://Martin@krischik.com
http://www.krischik.com




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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
  2003-01-20 14:57 ` Damien CARBONNE
  2003-01-20 15:08 ` Stephen Leake
@ 2003-01-20 21:27 ` Victor Porton
  2003-01-21 12:35   ` Georg Bauhaus
                     ` (2 more replies)
  2003-01-21 13:10 ` Victor Porton
                   ` (2 subsequent siblings)
  5 siblings, 3 replies; 16+ messages in thread
From: Victor Porton @ 2003-01-20 21:27 UTC (permalink / raw)


In article <uiswjamfw.fsf@nasa.gov>,
	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
> porton@ex-code.com (Victor Porton) writes:
> 
>> Is it possible to cause Gnat to not do unnecessary copying
>> when calling this fynction?
>> 
>> If T is a controlled type, it calls Adjust 3(!) times even if I
>> compile with optimization ("-O3 -gnatN").
> 
> Post complete code that demonstrates this; maybe I'll have other
> suggestions. 
> 
>> It is in Gnat-3.14p. May be it was fixed in Gnat-3.2/3.15p?

I don't see any relevant problems about inlining even if all
warnings are on (however it does not inline an unrelevant library 
function). But Adjust is called 3 times :-(

-- m.adb --
with P;
procedure M is
    function Pass(X: P.T) return P.T is
        pragma Inline(Pass);
    begin
	return X;
    end;
    X: P.T;
    Y: P.T := Pass(X);
begin
    null;
end M;

-- p.ads --
with Ada.Finalization;
package P is
    type T is new Ada.Finalization.Controlled with null record;
    procedure Adjust(Object: in out T);
end P;

-- p.adb --
with Ada.Text_IO;
package body P is
    procedure Adjust (Object: in out T) is
    begin
       Ada.Text_IO.Put_Line("QWER");
    end Adjust;
end P;



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

* Re: Eliminating copying in Gnat
  2003-01-20 21:27 ` Victor Porton
@ 2003-01-21 12:35   ` Georg Bauhaus
  2003-01-21 19:21     ` Robert A Duff
  2003-01-21 14:52   ` Stephen Leake
  2003-01-21 18:49   ` Martin Krischik
  2 siblings, 1 reply; 16+ messages in thread
From: Georg Bauhaus @ 2003-01-21 12:35 UTC (permalink / raw)


Victor Porton <porton@ex-code.com> wrote:
:  But Adjust is called 3 times :-(

Are compilers allowed to optimize away an Adjust?
Might Adjust not have side effects?

-- Georg



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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
                   ` (2 preceding siblings ...)
  2003-01-20 21:27 ` Victor Porton
@ 2003-01-21 13:10 ` Victor Porton
  2003-01-21 18:40 ` Victor Porton
  2003-01-21 20:54 ` Victor Porton
  5 siblings, 0 replies; 16+ messages in thread
From: Victor Porton @ 2003-01-21 13:10 UTC (permalink / raw)


In article <b0jer0$ma4$1@a1-hrz.uni-duisburg.de>,
	Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de> writes:
> Victor Porton <porton@ex-code.com> wrote:
>:  But Adjust is called 3 times :-(
> 
> Are compilers allowed to optimize away an Adjust?
> Might Adjust not have side effects?

Yes, Standard permits throwing Adjust away in certain situations.

Not 100% sure about side effects, but I think these are permitted.



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

* Re: Eliminating copying in Gnat
@ 2003-01-21 13:31 Grein, Christoph
  2003-01-21 18:54 ` Randy Brukardt
  2003-01-21 19:02 ` Georg Bauhaus
  0 siblings, 2 replies; 16+ messages in thread
From: Grein, Christoph @ 2003-01-21 13:31 UTC (permalink / raw)
  To: comp.lang.ada

> Victor Porton <porton@ex-code.com> wrote:
> :  But Adjust is called 3 times :-(
> 
> Are compilers allowed to optimize away an Adjust?
> Might Adjust not have side effects?

Read RM 7.6. There is a detailed description of the model how functions deal 
with their intermediate objects. Optimizations are allowed so that the number of 
calls of Adjust may vary.

Gnat has three calls, Rational Apex two in this case.



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

* Re: Eliminating copying in Gnat
  2003-01-20 21:27 ` Victor Porton
  2003-01-21 12:35   ` Georg Bauhaus
@ 2003-01-21 14:52   ` Stephen Leake
  2003-01-21 18:49   ` Martin Krischik
  2 siblings, 0 replies; 16+ messages in thread
From: Stephen Leake @ 2003-01-21 14:52 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> In article <uiswjamfw.fsf@nasa.gov>,
> 	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
> > Post complete code that demonstrates this; maybe I'll have other
> > suggestions. 
> > 
> >> It is in Gnat-3.14p. May be it was fixed in Gnat-3.2/3.15p?
> 
> I don't see any relevant problems about inlining even if all
> warnings are on (however it does not inline an unrelevant library 
> function). But Adjust is called 3 times :-(

I compiled the code with GNAT 3.15a1, with -gnatwa -gnatn (all
warnings, inlining enabled). Still gives three calls to Adjust.

-- 
-- Stephe



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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
                   ` (3 preceding siblings ...)
  2003-01-21 13:10 ` Victor Porton
@ 2003-01-21 18:40 ` Victor Porton
  2003-01-21 20:54 ` Victor Porton
  5 siblings, 0 replies; 16+ messages in thread
From: Victor Porton @ 2003-01-21 18:40 UTC (permalink / raw)


In article <uiswi5zcm.fsf@nasa.gov>,
	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
> porton@ex-code.com (Victor Porton) writes:
> 
>> In article <uiswjamfw.fsf@nasa.gov>,
>> 	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
>> >> It is in Gnat-3.14p. May be it was fixed in Gnat-3.2/3.15p?
>> 
>> I don't see any relevant problems about inlining even if all
>> warnings are on (however it does not inline an unrelevant library 
>> function). But Adjust is called 3 times :-(
> 
> I compiled the code with GNAT 3.15a1, with -gnatwa -gnatn (all
> warnings, inlining enabled). Still gives three calls to Adjust.

I should "-gnatN -O3" instead "-gnatn" (if the same systax as 3.14).



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

* Re: Eliminating copying in Gnat
  2003-01-20 21:27 ` Victor Porton
  2003-01-21 12:35   ` Georg Bauhaus
  2003-01-21 14:52   ` Stephen Leake
@ 2003-01-21 18:49   ` Martin Krischik
  2 siblings, 0 replies; 16+ messages in thread
From: Martin Krischik @ 2003-01-21 18:49 UTC (permalink / raw)


On Tue, 21 Jan 2003 02:27:56 +0500, Victor Porton wrote:

> Path:
> 	news.t-online.com!newsmm00.sul.t-online.com!newsfeed01.sul.t-online.de!t-on
> 	line.de!newsfeed.stueberl.de!news-peer.gradwell.net!not-for-mail
> Message-ID: <3e2c8625$0$33922$bed64819@news.gradwell.net>
> From: porton@ex-code.com (Victor Porton)
> Newsgroups: comp.lang.ada
> Subject: Re: Eliminating copying in Gnat
> Date: Tue, 21 Jan 2003 02:27:56 +0500
> References: <3e2b7777$0$33930$bed64819@news.gradwell.net>
> Lines: 48
> Organization: Extreme Code Software (http://ex-code.com)
> X-Newsreader: knews 1.0b.1
> X-URL: http://www.ex-code.com/
> NNTP-Posting-Date: 20 Jan 2003 23:28:37 GMT
> NNTP-Posting-Host: 195.149.39.13
> X-Trace: 1043105317 news.gradwell.net 33922 mail2news/195.149.39.13
> X-Complaints-To: news-abuse@gradwell.net
> Xref: linux1.krischik.com comp.lang.ada:410
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> 
> 
> In article <uiswjamfw.fsf@nasa.gov>,
> 	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:

> I don't see any relevant problems about inlining even if all
> warnings are on

Pass is not metioned inside the m.ads (the is no m.ads?!) so the position of
the pragma inline should be ok.

> (however it does not inline an unrelevant library 
> function). But Adjust is called 3 times :-(

3 is indeed strange. I would expect 2. One into the function and one for
the return.
 
> -- m.adb --
> with P;
> procedure M is
>     function Pass(X: P.T) return P.T is
>         pragma Inline(Pass);
>     begin
> 	return X;
>     end;
>     X: P.T;
>     Y: P.T := Pass(X);

are you shure that all Adjust are part of this statement. Prehaps you like
to ann anther line: 

      Z: P:T := Pass(x);

and see of the count goes up to 5 or 6.

> begin
>     null;
> end M;
> 
> -- p.ads --
> with Ada.Finalization;
> package P is
>     type T is new Ada.Finalization.Controlled with null record;
>     procedure Adjust(Object: in out T);
> end P;
> 
> -- p.adb --
> with Ada.Text_IO;
> package body P is
>     procedure Adjust (Object: in out T) is
>     begin
>        Ada.Text_IO.Put_Line("QWER");
>     end Adjust;
> end P;

-- 
Martin Krischik
mailto://Martin@krischik.com
http://www.martin.krischik.com




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

* Re: Eliminating copying in Gnat
  2003-01-21 13:31 Grein, Christoph
@ 2003-01-21 18:54 ` Randy Brukardt
  2003-01-21 19:02 ` Georg Bauhaus
  1 sibling, 0 replies; 16+ messages in thread
From: Randy Brukardt @ 2003-01-21 18:54 UTC (permalink / raw)


Grein, Christoph wrote in message ...
>> Victor Porton <porton@ex-code.com> wrote:
>> :  But Adjust is called 3 times :-(
>>
>> Are compilers allowed to optimize away an Adjust?
>> Might Adjust not have side effects?
>
>Read RM 7.6. There is a detailed description of the model how functions
deal
>with their intermediate objects. Optimizations are allowed so that the
number of
>calls of Adjust may vary.
>
>Gnat has three calls, Rational Apex two in this case.

Word of warning: that description in 7.6 is pretty bad; it contains
several errors that both allow too much optimization and too little.
This is fixed by AI-147 (WG9 approved in December).

The basic summary: There is no optimization of calls for limited types.
For non-limited types, removal of temporaries and their associated pairs
of Adjust-Finalization operations is allowed. Dead variables with
trivial Initialize also can be removed.

In any case, this is an OPTIMIZATION. There is no requirement that a
compiler do any optimization here. (Given how dangerous and confused the
RM rules are, I'm not surprised if an implementor doesn't do any.)

This area was a major problem for Claw, as at least one implementation
did implement the RM rules as written, which makes it impossible to
adjust pointers to objects during an assignment. That implementation has
thankfully been fixed.

              Randy.





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

* Re: Eliminating copying in Gnat
  2003-01-21 13:31 Grein, Christoph
  2003-01-21 18:54 ` Randy Brukardt
@ 2003-01-21 19:02 ` Georg Bauhaus
  1 sibling, 0 replies; 16+ messages in thread
From: Georg Bauhaus @ 2003-01-21 19:02 UTC (permalink / raw)


Grein, Christoph <christoph.grein@eurocopter.com> wrote:
: Read RM 7.6.
Thank you.



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

* Re: Eliminating copying in Gnat
  2003-01-21 12:35   ` Georg Bauhaus
@ 2003-01-21 19:21     ` Robert A Duff
  0 siblings, 0 replies; 16+ messages in thread
From: Robert A Duff @ 2003-01-21 19:21 UTC (permalink / raw)


Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de> writes:

> Victor Porton <porton@ex-code.com> wrote:
> :  But Adjust is called 3 times :-(
> 
> Are compilers allowed to optimize away an Adjust?

Yes.  There is an AI about exactly when it can.

> Might Adjust not have side effects?

Yes, but they better be the sort of side effects that are benign under
the allowed optimizations.  For example, if the type is doing reference
counting, the compiler can optimize away matched pairs of
increment/decrement.  But if Adjust prints "Hello, world.",
the programmer should not expect to see an exact number of
"Hello, world."'s printed on the screen.

- Bob



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

* Re: Eliminating copying in Gnat
  2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
                   ` (4 preceding siblings ...)
  2003-01-21 18:40 ` Victor Porton
@ 2003-01-21 20:54 ` Victor Porton
  5 siblings, 0 replies; 16+ messages in thread
From: Victor Porton @ 2003-01-21 20:54 UTC (permalink / raw)


In article <3e2da295$0$33924$bed64819@news.gradwell.net>,
	porton@ex-code.com (Victor Porton) writes:
> In article <uiswi5zcm.fsf@nasa.gov>,
> 	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
>> porton@ex-code.com (Victor Porton) writes:
>> 
>> I compiled the code with GNAT 3.15a1, with -gnatwa -gnatn (all
>> warnings, inlining enabled). Still gives three calls to Adjust.
> 
> I should "-gnatN -O3" instead "-gnatn" (if the same systax as 3.14).

"You should", not "I should", my misspelling.



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

end of thread, other threads:[~2003-01-21 20:54 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-20  4:12 Eliminating copying in Gnat Victor Porton
2003-01-20 14:57 ` Damien CARBONNE
2003-01-20 15:03   ` Lutz Donnerhacke
2003-01-20 18:34   ` Martin Krischik
2003-01-20 15:08 ` Stephen Leake
2003-01-20 21:27 ` Victor Porton
2003-01-21 12:35   ` Georg Bauhaus
2003-01-21 19:21     ` Robert A Duff
2003-01-21 14:52   ` Stephen Leake
2003-01-21 18:49   ` Martin Krischik
2003-01-21 13:10 ` Victor Porton
2003-01-21 18:40 ` Victor Porton
2003-01-21 20:54 ` Victor Porton
  -- strict thread matches above, loose matches on Subject: below --
2003-01-21 13:31 Grein, Christoph
2003-01-21 18:54 ` Randy Brukardt
2003-01-21 19:02 ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox