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 C5E523858418 for ; Sun, 21 Jan 2024 20:41:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C5E523858418 Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C5E523858418 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=116.202.254.214 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705869679; cv=none; b=cGxM3RC/JD30q7YRDpGnzrUgPETpWVITWrPLw+TYxkIzJq9JXb88CsNTsPlWTJkKaXC0FYz6pry95VS1yyL/K6bfD8i3SfhilgcF3tZ4MXlYs5yvkkDYO11FMr+BeDHUclLu5LS3Fy1EbJNTE/xj/Ig8XXqx+slprh067COgfBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705869679; c=relaxed/simple; bh=MQlxW3zaxxWD6mQbOySzegfmhaQGweGG//dvvg1w2No=; h=To:From:Subject:Date:Message-ID:Mime-Version; b=RM8ncGJBbLQuNTn/eMlhK0RLpPCt7vPSbU+vyssBwT6DG2hCXZkICfxsRWUtzdaTed6Lc0+1ctiX+ECut7MlU5CZz3NUpKTfaBBDi8upu0StUkjP7KFqM03ITrXXNq5fmmIZ6ltYIWVx1y/pO5BfEnqfIHPqPt4cODNtzpFcFz4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1rRed1-0003KJ-Nm for gcc-patches@gcc.gnu.org; Sun, 21 Jan 2024 21:41:15 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-patches@gcc.gnu.org From: Harald Anlauf Subject: Re: [PATCH] Fortran: passing of optional scalar arguments with VALUE attribute [PR113377] Date: Sun, 21 Jan 2024 21:41:07 +0100 Message-ID: References: <818b7f17-2d2d-476d-af31-14a0702f53bf@orange.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla Thunderbird Content-Language: en-US In-Reply-To: <818b7f17-2d2d-476d-af31-14a0702f53bf@orange.fr> 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,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: <20240121204107.1k4G2w599blASCcQx3J15MiAfc9LRp0jOQkha0LvodI@z> Hi Mikael! Am 21.01.24 um 11:50 schrieb Mikael Morin: > Hello, > > Le 20/01/2024 à 22:58, Harald Anlauf a écrit : >> Dear all, >> >> here's the first part of an attempt to fix issues with optional >> dummy arguments as actual arguments to optional dummies.  This patch >> rectifies the case of scalar dummies with the VALUE attribute, >> which in gfortran's argument passing convention are passed on the >> stack when they are of intrinsic type, and have a hidden variable >> for the presence status. >> >> The testcase tries to cover valid combinations of actual and dummy >> argument.  A few tests that are not standard-conforming but would >> still work with gfortran (due to the argument passing convention) >> are left there but commented out with a pointer to the standard >> (thanks, Mikael!). >> >> Regtested on x86_64-pc-linux-gnu.  OK for mainline? >> > Well, not yet. > >> >> diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc >> index 9dd1f4086f4..2f47a75955c 100644 >> --- a/gcc/fortran/trans-expr.cc >> +++ b/gcc/fortran/trans-expr.cc >> @@ -6526,6 +6526,10 @@ gfc_conv_procedure_call (gfc_se * se, >> gfc_symbol * sym, >>                  gfc_init_se (&argse, NULL); >>                  argse.want_pointer = 1; >>                  gfc_conv_expr (&argse, e); >> +                if (e->symtree->n.sym->attr.dummy >> +                && POINTER_TYPE_P (TREE_TYPE (argse.expr))) >> +                  argse.expr = gfc_build_addr_expr (NULL_TREE, >> +                                argse.expr); > > The second part of the condition looks superfluous: if > argse.want_pointer was set, we can expect to get a pointer result. > > But more important, I don't understand the need for this whole part, the > new test seems to pass without it. > And here is an example that regresses with it. > > program p >   type :: t >     integer, allocatable :: c >   end type >   call s2(t()) > contains >   subroutine s1(a) >     integer, value, optional :: a >     if (present(a)) stop 1 >   end subroutine >   subroutine s2(a) >     type(t) :: a >     call s1(a%c) >   end subroutine > end program Thanks for this example! I've taken the liberty to add a slightly extended version of it to the testcase. I was taken astray by the attempt to handle the (invalid by the standard) variant of passing an absent allocatable scalar to an optional scalar dummy with the value attribute under since we use a hidden variable for the present status. Without the code above there is an unprotected pointer dereference. I think that it still could be done, but it is probably not worth it. So I followed your suggestion and removed that part. > >>                  cond = fold_convert (TREE_TYPE (argse.expr), >>                           null_pointer_node); >>                  cond = fold_build2_loc (input_location, NE_EXPR, >> @@ -7256,6 +7260,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol >> * sym, >>            && e->symtree->n.sym->attr.optional >>            && (((e->rank != 0 && elemental_proc) >>             || e->representation.length || e->ts.type == BT_CHARACTER >> +           || (e->rank == 0 && e->symtree->n.sym->attr.value) > > This looks good. > >>             || (e->rank != 0 >>                 && (fsym == NULL >>                 || (fsym->as >> diff --git a/gcc/testsuite/gfortran.dg/optional_absent_9.f90 >> b/gcc/testsuite/gfortran.dg/optional_absent_9.f90 >> new file mode 100644 >> index 00000000000..495a6c00d7f >> --- /dev/null >> +++ b/gcc/testsuite/gfortran.dg/optional_absent_9.f90 >> @@ -0,0 +1,324 @@ >> +! { dg-do run } >> +! PR fortran/113377 >> +! >> +! Test passing of missing optional scalar dummies of intrinsic type >> + >> +module m_int >> +  implicit none >> +contains >> +  subroutine test_int () >> +    integer :: k = 1 >> +    call one     (k) >> +    call one_val (k) >> +    call one_all (k) >> +    call one_ptr (k) >> +  end >> + >> +  subroutine one (i, j) >> +    integer, intent(in)           :: i >> +    integer             ,optional :: j >> +    integer, allocatable :: aa >> +    integer, pointer     :: pp => NULL() >> +    if (present (j)) error stop "j is present" >> +    call two     (i, j) >> +    call two_val (i, j) >> +    call two     (i, aa) >> +    call two     (i, pp) > > To be complete, you could check two_val(i, aa) and two_val(i, pp) as well. > Both seem to pass already without the patch, so not absolutely needed. > Your call. It is already contained in testcase gfortran.dg/value_optional_1.f90, (see call sub there), but then it may be helpful to have it here too. Thus added. >> +  end >> + > > I think the patch is OK with the first trans-expr.cc hunk removed. > Thanks. That's what I have done and pushed as r14-8317-g68862e5c75ef0e. > Mikael Thanks for the review! Harald