comp.lang.ada
 help / color / mirror / Atom feed
* GNAT 4.9 - missing optimization feature?
@ 2014-08-05 22:55 Victor Porton
  2014-08-06 14:19 ` Victor Porton
  0 siblings, 1 reply; 7+ messages in thread
From: Victor Porton @ 2014-08-05 22:55 UTC (permalink / raw)


The below program, compiled with GNAT 4.9, calls Adjust two times when 
copying a T1 object.

But it does the same operations with a T2 object without calling Adjust.

So calling Adjust on a T1 object is here redundant and can be optimized away 
for greater performance. If I recall correctly, Ada Reference Manual allows 
this kind of optimization.

So GNAT is not as good as I expected, isn't it?

with Ada.Finalization;
with Ada.Text_IO;

procedure Main is

   type T1 is new Ada.Finalization.Controlled with null record;
   type T2 is new Ada.Finalization.Limited_Controlled with null record;
   
   overriding procedure Adjust(Object: in out T1) is
   begin
      Ada.Text_IO.Put_Line("Adjust");
   end;

   function F return T1 is
   begin
      return (Ada.Finalization.Controlled with null record);
   end;
   
   function F return T2 is
   begin
      return (Ada.Finalization.Limited_Controlled with null record);
   end;
   
   X: T1 := F;
   Y: T2 := F;

begin
   null;
end;

-- 
Victor Porton - http://portonvictor.org

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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-05 22:55 GNAT 4.9 - missing optimization feature? Victor Porton
@ 2014-08-06 14:19 ` Victor Porton
  2014-08-06 15:10   ` J-P. Rosen
  2014-08-06 15:15   ` Adam Beneschan
  0 siblings, 2 replies; 7+ messages in thread
From: Victor Porton @ 2014-08-06 14:19 UTC (permalink / raw)


Victor Porton wrote:

> The below program, compiled with GNAT 4.9, calls Adjust two times when
> copying a T1 object.
> 
> But it does the same operations with a T2 object without calling Adjust.
> 
> So calling Adjust on a T1 object is here redundant and can be optimized
> away for greater performance. If I recall correctly, Ada Reference Manual
> allows this kind of optimization.
> 
> So GNAT is not as good as I expected, isn't it?
> 
> with Ada.Finalization;
> with Ada.Text_IO;
> 
> procedure Main is
> 
>    type T1 is new Ada.Finalization.Controlled with null record;
>    type T2 is new Ada.Finalization.Limited_Controlled with null record;
>    
>    overriding procedure Adjust(Object: in out T1) is
>    begin
>       Ada.Text_IO.Put_Line("Adjust");
>    end;
> 
>    function F return T1 is
>    begin
>       return (Ada.Finalization.Controlled with null record);
>    end;
>    
>    function F return T2 is
>    begin
>       return (Ada.Finalization.Limited_Controlled with null record);
>    end;
>    
>    X: T1 := F;
>    Y: T2 := F;
> 
> begin
>    null;
> end;

Is it true that this kind of optimization is legitimate accordingly ARM?

-- 
Victor Porton - http://portonvictor.org

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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-06 14:19 ` Victor Porton
@ 2014-08-06 15:10   ` J-P. Rosen
  2014-08-06 15:16     ` Adam Beneschan
  2014-08-06 15:15   ` Adam Beneschan
  1 sibling, 1 reply; 7+ messages in thread
From: J-P. Rosen @ 2014-08-06 15:10 UTC (permalink / raw)


Le 06/08/2014 16:19, Victor Porton a écrit :
> The below program, compiled with GNAT 4.9, calls Adjust two times when
>> copying a T1 object.
>> 
>> But it does the same operations with a T2 object without calling Adjust.
>> 
For the simple reason that there is no Adjust defined for objects of
type T2, and there can't be because T2 is limited (derived from
Limited_Controlled).

So what's the point?

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-06 14:19 ` Victor Porton
  2014-08-06 15:10   ` J-P. Rosen
@ 2014-08-06 15:15   ` Adam Beneschan
  1 sibling, 0 replies; 7+ messages in thread
From: Adam Beneschan @ 2014-08-06 15:15 UTC (permalink / raw)


On Wednesday, August 6, 2014 7:19:08 AM UTC-7, Victor Porton wrote:
> Victor Porton wrote:
> 
> > The below program, compiled with GNAT 4.9, calls Adjust two times when
> > copying a T1 object.
> 
> > But it does the same operations with a T2 object without calling Adjust.
> 
> > So calling Adjust on a T1 object is here redundant and can be optimized
> > away for greater performance. If I recall correctly, Ada Reference Manual
> > allows this kind of optimization.

> Is it true that this kind of optimization is legitimate accordingly ARM?

Yes.  See 7.6(17.1 - 17.4).  You're using a function call to initialize an object.  This section says that an object must be "built in place" if the type is limited, which is why there is no copying and no Adjust for T2.  (Limited_Controlled doesn't have an Adjust procedure, of course.)  7.6(17.4) says that when the two cases where an object *must* be built in place don't apply, "it is unspecified whether the anonymous object is built in place".  That means that the compiler could generate code to put the function result directly in T1 (the optimization), or it could generate code to put the function result in some other object and then copy.

                                 -- Adam


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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-06 15:10   ` J-P. Rosen
@ 2014-08-06 15:16     ` Adam Beneschan
  2014-08-06 15:35       ` J-P. Rosen
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2014-08-06 15:16 UTC (permalink / raw)


On Wednesday, August 6, 2014 8:10:52 AM UTC-7, J-P. Rosen wrote:

> For the simple reason that there is no Adjust defined for objects of
> type T2, and there can't be because T2 is limited (derived from
> Limited_Controlled).
> 
> So what's the point?

The point is that code *could* be generated not to call Adjust in the T1 case, but GNAT doesn't do so.

                                  -- Adam

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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-06 15:16     ` Adam Beneschan
@ 2014-08-06 15:35       ` J-P. Rosen
  2014-08-06 15:44         ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: J-P. Rosen @ 2014-08-06 15:35 UTC (permalink / raw)


Le 06/08/2014 17:16, Adam Beneschan a écrit :
>> So what's the point?
> The point is that code *could* be generated not to call Adjust in the T1 case, but GNAT doesn't do so.

Yes, but I was asking the OP, where he assumed that the non-existent
adjust was not called due to optimization...

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: GNAT 4.9 - missing optimization feature?
  2014-08-06 15:35       ` J-P. Rosen
@ 2014-08-06 15:44         ` Adam Beneschan
  0 siblings, 0 replies; 7+ messages in thread
From: Adam Beneschan @ 2014-08-06 15:44 UTC (permalink / raw)


On Wednesday, August 6, 2014 8:35:27 AM UTC-7, J-P. Rosen wrote:

> > The point is that code *could* be generated not to call Adjust in the T1 case, but GNAT doesn't do so.

> Yes, but I was asking the OP, where he assumed that the non-existent
> adjust was not called due to optimization...

That isn't how I interpreted his question.  But yes, perhaps I should have let him answer for himself.

                            -- Adam

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

end of thread, other threads:[~2014-08-06 15:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-05 22:55 GNAT 4.9 - missing optimization feature? Victor Porton
2014-08-06 14:19 ` Victor Porton
2014-08-06 15:10   ` J-P. Rosen
2014-08-06 15:16     ` Adam Beneschan
2014-08-06 15:35       ` J-P. Rosen
2014-08-06 15:44         ` Adam Beneschan
2014-08-06 15:15   ` Adam Beneschan

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