From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 20E9F385700D for ; Tue, 4 Jul 2023 19:00:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20E9F385700D Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1qGlGJ-0001y0-27 for gcc-patches@gcc.gnu.org; Tue, 04 Jul 2023 21:00:31 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-patches@gcc.gnu.org From: Harald Anlauf Subject: Re: [PATCH] Fortran: fixes for procedures with ALLOCATABLE,INTENT(OUT) arguments [PR92178] Date: Tue, 4 Jul 2023 21:00:23 +0200 Message-ID: References: <5a5306ae-0db1-c7e2-e744-a3beced17636@orange.fr> <3adc2904-9876-74d6-2b5d-3cc1896866c3@gmx.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------s8o5nY6ocPM2ejf4Ta0VcmX3" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: en-US In-Reply-To: Cc: fortran@gcc.gnu.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,BODY_8BITS,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Message-ID: <20230704190023.khharryC8WalmMCv2bftZM6Z8mjDDkVQNSbT2lnnHAs@z> This is a multi-part message in MIME format. --------------s8o5nY6ocPM2ejf4Ta0VcmX3 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Mikael, all, I think I've found it: there is a call to gfc_conv_class_to_class that - according to a comment - does a repackaging to a class array. Deferring that repackaging along with the deallocation not only fixes the regression, but also the cases I tested. Attached is a "sneak preview", hoping that the experts (Paul, Mikael, ...) can tell if I am going down the wrong road. I'll wrap up all pieces and resubmit when the dust settles. We can then address the other findings later. Harald Am 04.07.23 um 15:35 schrieb Mikael Morin: > Le 03/07/2023 à 22:49, Harald Anlauf a écrit : >> Hi Mikael, >> >> Am 03.07.23 um 13:46 schrieb Mikael Morin: >>> These look good, but I'm surprised that there is no similar change at >>> the 6819 line. >>> This is the class array actual vs class array dummy case. >>> It seems to be checked by the "bar" subroutine in your testcase, except >>> that the intent(out) argument comes last there, whereas it was coming >>> first with the original testcases in the PR. >>> Can you double check? >> >> I believe I tried that before and encountered regressions. >> The change >> >> diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc >> index 16e8f037cfc..43e013fa720 100644 >> --- a/gcc/fortran/trans-expr.cc >> +++ b/gcc/fortran/trans-expr.cc >> @@ -6844,7 +6844,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * >> sym, >>                    else >>                      tmp = gfc_finish_block (&block); >> >> -                 gfc_add_expr_to_block (&se->pre, tmp); >> +//               gfc_add_expr_to_block (&se->pre, tmp); >> +                 gfc_add_expr_to_block (&dealloc_blk, tmp); >>                  } >> >>                /* The conversion does not repackage the reference to a >> class >> >> regresses on: >> gfortran.dg/class_array_16.f90 >> gfortran.dg/finalize_12.f90 >> gfortran.dg/optional_class_1.f90 >> >> A simplified testcase for further study: >> >> program p >>    implicit none >>    class(*),  allocatable :: c(:) >>    c = [3, 4] >>    call bar (allocated (c), c, allocated (c)) >>    if (allocated (c)) stop 14 >> contains >>    subroutine bar (alloc, x, alloc2) >>      logical :: alloc, alloc2 >>      class(*), allocatable, intent(out) :: x(:) >>      if (allocated (x)) stop 5 >>      if (.not. alloc)   stop 6 >>      if (.not. alloc2)  stop 16 >>    end subroutine bar >> end >> >> (This fails in a different place for the posted patch and for >> the above trial change.  Need to go to the drawing board...) >> > I've had a quick look. > > The code originally generated looks like: > >     D.4343 = (void *[0:] * restrict) c._data.data != 0B; >     if (c._data.data != 0B) >       // free c._data.data >     c._data.data = 0B; >     ... >     class.3._data = c._data; >     ... >     D.4345 = (void *[0:] * restrict) c._data.data != 0B; >     bar (&D.4343, &class.3, &D.4345); > > this fails because D.4345 has the wrong value. > With your change, it becomes: > >     D.4343 = (void *[0:] * restrict) c._data.data != 0B; >     ... >     class.3._data = c._data; >     ... >     D.4345 = (void *[0:] * restrict) c._data.data != 0B; >     if (c._data.data != 0B) >       // free c._data.data >     c._data.data = 0B; >     bar (&D.4343, &class.3, &D.4345); > > and then it is class.3._data that has the wrong value. > So basically the initialization of class.3 should move with the > deallocation. > > I can reproduce a similar problem with your unmodified patch on the > following variant: > > program p >   implicit none >   class(*),  allocatable :: c >   c = 3 >   call bar (c, allocated (c)) >   if (allocated (c)) stop 14 > contains >   subroutine bar (x, alloc2) >     logical :: alloc, alloc2 >     class(*), allocatable, intent(out) :: x(..) >     if (allocated (x)) stop 5 >     if (.not. alloc)   stop 6 >     if (.not. alloc2)  stop 16 >   end subroutine bar > end > > > --------------s8o5nY6ocPM2ejf4Ta0VcmX3 Content-Type: text/x-patch; charset=UTF-8; name="pr92178-v2-partial.patch" Content-Disposition: attachment; filename="pr92178-v2-partial.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL2djYy9mb3J0cmFuL3RyYW5zLWV4cHIuY2MgYi9nY2MvZm9ydHJhbi90 cmFucy1leHByLmNjCmluZGV4IDE2ZThmMDM3Y2ZjLi5hNjhjOGQzM2FjYyAxMDA2NDQKLS0t IGEvZ2NjL2ZvcnRyYW4vdHJhbnMtZXhwci5jYworKysgYi9nY2MvZm9ydHJhbi90cmFucy1l eHByLmNjCkBAIC02ODA0LDY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