From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) by sourceware.org (Postfix) with ESMTPS id 946AF3858D38; Thu, 22 Feb 2024 00:52:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 946AF3858D38 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=troutmask.apl.washington.edu Authentication-Results: sourceware.org; spf=none smtp.mailfrom=troutmask.apl.washington.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 946AF3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=128.95.76.21 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708563134; cv=none; b=bVCivQLwm3/9FuGxa/TMp+2/mbgvsLCtSx4c+x3aPI/LZJvxbWgSA4udy7FP1J9kO1ebNXXVT/NEgNFoTGRzmzeQrMTP8hamzy1WbcDvaGDxFnfpRYKl0jTazqAczGo2+2UkYSfZcfKiMSoG6y5c0bdJERZFiHal3kzfgfr9W34= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708563134; c=relaxed/simple; bh=msAOnD4JkuAoFWMAChSciZDb3O3wfsmn24r+hJm0cx0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=kVweMFvDtxFz7ijgo28fJ08rfpfBYKIldz+z2/jeb0nRdFnUpGrU2ozMbRumwqunCTQH1GltjjvdqumCkemBN/wx6IdI3KdTdSC/JVH7i5ruKE2F9/+UaKT6X5o3SxDja1NiQl6rc9crGnnyAstTG3xV6nKjpcwXMCSAKZbGqIk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.17.1/8.17.1) with ESMTP id 41M0q9HA069478; Wed, 21 Feb 2024 16:52:09 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) DKIM-Filter: OpenDKIM Filter v2.10.3 troutmask.apl.washington.edu 41M0q9HA069478 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=troutmask.apl.washington.edu; s=troutmask; t=1708563130; bh=msAOnD4JkuAoFWMAChSciZDb3O3wfsmn24r+hJm0cx0=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=LTbKfpPli5Vuukj9QNUcZb54n8w+ILQLtAIpLJ5Sy3vP1W+/GYt9ndyujgXMXKt5Q bxRYj698DRcWClorUHqYe4aLPbqWWzsO6JZe4jixkTW9yU0YsGtBuzDWv5yeTdby35 38vX36PYYPxUbagveOUWtYwWwpPf3iu59cclDw9GjBrb+EPHuom4BdP3efr0CnRjEZ xmTbQdyZChYKZZk3ZOly/MvVuLJxkx7EUD/x/4Qql3BcBwEWmp+UKE0ffltYrNod5Y z/3IM/60F0ikpnRG7KIIWPXo1YNQJcdztslRXDQS6C/+5i2lAJ7ZbSMF2LXfjyQh4p mpQO/e8TNoTUQ== Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.17.1/8.17.1/Submit) id 41M0q9xJ069477; Wed, 21 Feb 2024 16:52:09 -0800 (PST) (envelope-from sgk) Date: Wed, 21 Feb 2024 16:52:09 -0800 From: Steve Kargl To: Harald Anlauf Cc: Jerry D , fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Fix fortran/PR114024 Message-ID: Reply-To: sgk@troutmask.apl.washington.edu References: <29ba08a7-8218-4591-8c3f-36c17090e497@gmail.com> <3444d912-2e79-4e16-a425-79810d161ebb@gmx.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="AG3fz1Uww/L3aE8o" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00,DKIM_INVALID,DKIM_SIGNED,GIT_PATCH_0,KAM_DMARC_STATUS,KAM_NUMSUBJECT,SPF_HELO_NONE,SPF_NONE,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: --AG3fz1Uww/L3aE8o Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Feb 21, 2024 at 01:42:32PM -0800, Steve Kargl wrote: > On Wed, Feb 21, 2024 at 10:20:43PM +0100, Harald Anlauf wrote: > > On 2/21/24 22:00, Steve Kargl wrote: > > > memleak vs ICE. I think I'll take one over the other. > > > Probably need to free code->expr3 before the copy. > > > > Yep. > > > > > I tried gfc_replace_expr in an earlier patch. It did not > > > work. I tried freeing code->expr3 before assigning the new expression. That leads to % gfcx -c ~/gcc/gccx/gcc/testsuite/gfortran.dg/allocate_with_source_28.f90 pid 69473 comm f951 has trashed its stack, killing gfortran: internal compiler error: Illegal instruction signal terminated program f951 If I don't free code->expr3 but simply assign the new expression from gfc_get_parentheses(), your example now compiles are executes are expected. It now allocate_with_source_28.f90. Caveat: I don't know how to test the CLASS uu. > > > > - it still fails on the following code, because the traversal > > > > of the refs is incomplete / wrong: > > > > > > > > program foo > > > > implicit none > > > > complex :: cmp(3) > > > > real, pointer :: pp(:) > > > > class(*), allocatable :: uu(:) > > > > type t > > > > real :: re > > > > real :: im > > > > end type t > > > > type u > > > > type(t) :: tt(3) > > > > end type u > > > > type(u) :: cc > > > > > > > > cmp = (3.45,6.78) > > > > cc% tt% re = cmp% re > > > > cc% tt% im = cmp% im > > > > allocate (pp, source = cc% tt% im) ! ICE > > > > > > cc%tt%im isn't a complex-part-ref, so this seems to > > > be a different (maybe related) issue. Does the code > > > compile with 'source = (cc%tt%im)'? If so, perhaps, > > > detecting a component reference and doing the simply > > > wrapping with parentheses can be done. > > > > Yes, that's why I tried to make up the above example. > > I think %re and %im are not too special, they work > > here pretty much like component refs elsewhere. > > > > I see. The %re and %im complex-part-ref correspond to > ref->u.i == INQUIRY_RE and INQUIRY_IM, respectively. > A part-ref for a user-defined type doesn't have an > INQUIRY_xxx, so we'll need to see if there is a way to > easily identify, e.g., cc%tt%re from your testcase. The attach patch uses ref->type == REF_COMPONENT to deal with the above code. -- Steve --AG3fz1Uww/L3aE8o Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="pr114024.diff" diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index 5247d3d39d7..414248fe2e5 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -6354,9 +6354,35 @@ gfc_trans_allocate (gfc_code * code, gfc_omp_namelist *omp_allocate) al = al->next) vtab_needed = (al->expr->ts.type == BT_CLASS); + /* When expr3 is a variable, i.e., a very simple expression, then + convert it once here. */ + gfc_init_se (&se, NULL); - /* When expr3 is a variable, i.e., a very simple expression, - then convert it once here. */ + + /* If one has source = z%re or z%im with z a complex array or + source = a%b%c where a or b is an array of a derived type, then + things can go sideways with the complex-part-refi or part-ref, so + wrap the entity in parentheses to force evaluation of an expression. + That is, the else-branch in the following if-else-stmt is entered. */ + + if (code->expr3->expr_type == EXPR_VARIABLE + && code->expr3->ts.type == BT_REAL + && code->expr3->ref) + { + gfc_ref *ref = code->expr3->ref; + + while (ref->next) + ref = ref->next; + + if (ref->u.i == INQUIRY_IM || ref->u.i == INQUIRY_RE + || ref->type == REF_COMPONENT) + { + gfc_expr *etmp = gfc_get_parentheses (code->expr3); + code->expr3 = gfc_copy_expr (etmp); + gfc_free_expr (etmp); + } + } + if (code->expr3->expr_type == EXPR_VARIABLE || code->expr3->expr_type == EXPR_ARRAY || code->expr3->expr_type == EXPR_CONSTANT) diff --git a/gcc/testsuite/gfortran.dg/allocate_with_source_27.f90 b/gcc/testsuite/gfortran.dg/allocate_with_source_27.f90 new file mode 100644 index 00000000000..d0f0f3c4a84 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_with_source_27.f90 @@ -0,0 +1,20 @@ +! +! { dg-do run } +! +! fortran/PR114024 +! https://github.com/fujitsu/compiler-test-suite +! Modified from Fortran/0093/0093_0130.f90 +! +program foo + implicit none + complex :: cmp(3) + real, allocatable :: xx(:), yy(:), zz(:) + cmp = (3., 6.78) + allocate(xx, source = cmp%re) ! This caused an ICE. + allocate(yy, source = cmp(1:3)%re) ! This caused an ICE. + allocate(zz, source = (cmp%re)) + if (any(xx /= [3., 3., 3.])) stop 1 + if (any(yy /= [3., 3., 3.])) stop 2 + if (any(zz /= [3., 3., 3.])) stop 3 +end program foo + diff --git a/gcc/testsuite/gfortran.dg/allocate_with_source_28.f90 b/gcc/testsuite/gfortran.dg/allocate_with_source_28.f90 new file mode 100644 index 00000000000..5eeeeb167cd --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_with_source_28.f90 @@ -0,0 +1,27 @@ +! +! { dg-do run } +! Testcase from Harald Anlauf +! fortran/pr114024 +! +program foo + + implicit none + + complex cmp(3) + real, pointer :: pp(:) + class(*), allocatable :: uu(:) + type t + real re, im + end type t + type u + type(t) tt(3) + end type u + type(u) :: cc + + cmp = (3.,4.) + cc%tt%re = cmp%re + cc%tt%im = cmp%im + allocate (pp, source = cc%tt%im) ! ICE + if (any(pp /= [4., 4., 4.])) stop 1 + allocate (uu, source = cc%tt%im) ! ICE +end --AG3fz1Uww/L3aE8o--