public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mikael Morin <morin-mikael@orange.fr>
To: Harald Anlauf <anlauf@gmx.de>, fortran <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Fortran: fixes for procedures with ALLOCATABLE,INTENT(OUT) arguments [PR92178]
Date: Fri, 7 Jul 2023 14:21:29 +0200	[thread overview]
Message-ID: <ca0a699a-bd7a-fe58-20fe-d75eac8e4d24@orange.fr> (raw)
In-Reply-To: <b568256d-a3e1-b88a-e1d3-a10d319bf9d8@gmx.de>

[-- Attachment #1: Type: text/plain, Size: 2391 bytes --]

Le 05/07/2023 à 22:36, Harald Anlauf a écrit :
> Hi Mikael,
> 
> Am 05.07.23 um 16:54 schrieb Mikael Morin:
>> Here is an example, admittedly artificial.  Fails with the above change,
>> but fails with master as well.
>>
>> program p
>>    implicit none
>>    type t
>>      integer :: i
>>    end type t
>>    type u
>>      class(t), allocatable :: ta(:)
>>    end type u
>>    type(u), allocatable, target :: c(:)
>>    c = [u([t(1), t(3)]), u([t(4), t(9)])]
>>    call bar (allocated (c(c(1)%ta(1)%i)%ta), c(c(1)%ta(1)%i)%ta,
>> allocated (c(c(1)%ta(1)%i)%ta))
>>    if (allocated(c(1)%ta)) stop 11
>>    if (.not. allocated(c(2)%ta)) stop 12
>> contains
>>    subroutine bar (alloc, x, alloc2)
>>      logical :: alloc, alloc2
>>      class(t), allocatable, intent(out) :: x(:)
>>      if (allocated (x)) stop 1
>>      if (.not. alloc)   stop 2
>>      if (.not. alloc2)  stop 3
>>    end subroutine bar
>> end
> 
> while it looks artificial, it is valid, and IMHO it is a beast...
> 
> I've played around and added another argument gfc_se *convse to
> gfc_conv_class_to_class in an attempt to implement what I thought
> you suggested (to get the .pre/.post separately), but in the end
> this did not lead to working code.  And the tree-dump for your
> example above is beyond what I can grasp.
> 
> I've noticed that my attempt does not properly handle the
> parmse.post; at least this is what the above example shows:
> there is a small part after the call to bar that should have
> been executed before that call, which I attribute to .post.
> But my attempts in moving that part regresses on a couple
> of testcases with class and intent(out).  I am at a loss now.
> 
All that I can see after the call is a reassignment of the original data 
and vptr pointers from the temporary class container.  They seem at 
their right place there. But part of the expression seems to be 
evaluated again, instead of being picked up from parmse.expr.

> I am attaching the latest version of my patch to give you or
> Paul or others the opportunity to see what is wrong or add the
> missing pieces.
> 
I'm attaching what I have (lightly) tested so far, which doesn't work.
It seems gfc_conv_class_to_class reevaluates part of the original 
expression, which is not correct after deallocation.
Will have a look again tonight.

Mikael


[-- Attachment #2: pr92178_tmp.diff --]
[-- Type: text/x-patch, Size: 1386 bytes --]

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index ebef1a36577..54249c9c615 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -6819,9 +6819,13 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 		  defer_to_dealloc_blk = true;
 		}
 
+	      gfc_se class_se = parmse;
+	      gfc_init_block (&class_se.pre);
+	      gfc_init_block (&class_se.post);
+
 	      /* The conversion does not repackage the reference to a class
 	         array - _data descriptor.  */
-	      gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
+	      gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
 				     fsym->attr.intent != INTENT_IN
 				     && (CLASS_DATA (fsym)->attr.class_pointer
 					 || CLASS_DATA (fsym)->attr.allocatable),
@@ -6831,9 +6835,10 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 				     CLASS_DATA (fsym)->attr.class_pointer
 				     || CLASS_DATA (fsym)->attr.allocatable);
 
-	      /* Defer repackaging after deallocation.  */
-	      if (defer_to_dealloc_blk)
-		gfc_add_block_to_block (&dealloc_blk, &parmse.pre);
+	      parmse.expr = class_se.expr;
+	      stmtblock_t *class_pre_block = defer_to_dealloc_blk ? &dealloc_blk : &parmse.pre;
+	      gfc_add_block_to_block (class_pre_block, &class_se.pre);
+	      gfc_add_block_to_block (&parmse.post, &class_se.post);
 	    }
 	  else
 	    {

  parent reply	other threads:[~2023-07-07 12:22 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-02 20:38 Harald Anlauf
2023-07-03 11:46 ` Mikael Morin
2023-07-03 20:49   ` Harald Anlauf
2023-07-03 20:49     ` Harald Anlauf
2023-07-03 23:56     ` Steve Kargl
2023-07-04  9:26       ` Mikael Morin
2023-07-04 15:50         ` Steve Kargl
2023-07-04 13:35     ` Mikael Morin
2023-07-04 19:00       ` Harald Anlauf
2023-07-04 19:00         ` Harald Anlauf
2023-07-04 19:37         ` Mikael Morin
2023-07-05 14:54           ` Mikael Morin
2023-07-05 20:36             ` Harald Anlauf
2023-07-05 20:36               ` Harald Anlauf
2023-07-07 12:21               ` Mikael Morin [this message]
2023-07-07 18:23                 ` Harald Anlauf
2023-07-07 18:23                   ` Harald Anlauf
2023-07-08 12:07                   ` Mikael Morin
2023-07-08 14:20                     ` Harald Anlauf
2023-07-08 14:20                       ` Harald Anlauf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ca0a699a-bd7a-fe58-20fe-d75eac8e4d24@orange.fr \
    --to=morin-mikael@orange.fr \
    --cc=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).