From patchwork Thu Jul 13 08:52:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 119695 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1690640vqm; Thu, 13 Jul 2023 02:08:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlFx0S27uB+gycq8Flza/FiTicGML8VVzyQdnfedB6Ybu1iX37tVDCqiWgYe7Nyo5TwzclRi X-Received: by 2002:a17:907:3fa0:b0:986:38ab:ef99 with SMTP id hr32-20020a1709073fa000b0098638abef99mr6808304ejc.9.1689239298385; Thu, 13 Jul 2023 02:08:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689239298; cv=none; d=google.com; s=arc-20160816; b=xbD7vrwOy+61V+cNUREH/XCgpRdrTKYg5/n+HjriZ5bSb8VolUWlQuDXmZqTQX1L0l bAGVQSBSlw5XDMSsCqAhRyU82aXxE4A04Yy9HSQ4pX+x5E9ZHYbb58MoBvmDC20JPdu/ A01JueYdaa8Q1U6nq1joWkA+JZMBsZW+eMzjolV1Fl48Hy5zfswxnBXH52KPsAwvMvEs YtJBaBHbhFex8on6ZcQqQCb29GfXFDfRHtuzmAYQRqC2ngt6wsh6Ets4tphDxjG8B2W6 0BwFP3EcvSVmzCP09fgMd1oyZJRBDMWEhOeovySG/GQXuxL9uf+1kRF6Q8hNVxOLE4yI 500Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=d5Zv/wV77niuEQ8Y4n/HzaRWpkwq6r5GWs3aTENHrpk=; fh=EAqAZnhg4AYtcBjfPm18lEF5V0R2rkI9MSWQf+svVaI=; b=EkbKpmNsTnJcFG/KuDXubgc1HMTtN3Pc7IkyiU9MPXswaFN6Z3vlTynl2OJ2ZbrVbF InlSKjc9/plaUNhH058FsAzX+3ntedVF/cCsi8lC1+AMd0VzhJTNXtOdFrSvla+v4z1o Rerbs8Ucjj9amZ2f0yXuPcu1MFEY9m1WswtUoBfv2GsIfJmBS6Fna9blD6gaCGwh3sHi tDejYGwCnucPpztBVryJF/6xYLpsU2f46MUeXr19qcf5Yx7zNYt2/bEmaQ0Rb6EypaDC j7b3ga9IwHyWTAXDN1UFwdcul519AVgyXIYUGPZeSrdaPW5l0sHmEa0EIqYAcAMd2HAe F+EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=XKlMrMwX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id rv6-20020a17090710c600b009933205fdb5si6579350ejb.141.2023.07.13.02.08.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 02:08:18 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=XKlMrMwX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A588E3852AD8 for ; Thu, 13 Jul 2023 08:58:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A588E3852AD8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689238730; bh=d5Zv/wV77niuEQ8Y4n/HzaRWpkwq6r5GWs3aTENHrpk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=XKlMrMwXxjDZiuqYKaN2r0QaJRqS9TBWsYpjOI/MJZpB4pSdvMp2USa4natYL7R48 O7SZpjnEk2CpjElSBSdSY0DrhaeG+lZWlELD0De2duHWMiR1N/OKz0WIqdBCPJvyli Pilakf9qwfW774lASV24P3gtb7P7wCHwADGV5Cag= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp.smtpout.orange.fr (smtp-14.smtpout.orange.fr [80.12.242.14]) by sourceware.org (Postfix) with ESMTPS id 3F7373857722 for ; Thu, 13 Jul 2023 08:52:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F7373857722 Received: from cyrano.home ([86.215.161.51]) by smtp.orange.fr with ESMTPA id Js3wqzoJKtImzJs45q0yWP; Thu, 13 Jul 2023 10:52:45 +0200 X-ME-Helo: cyrano.home X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Thu, 13 Jul 2023 10:52:45 +0200 X-ME-IP: 86.215.161.51 To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 14/14] fortran: Pass pre-calculated class container argument [pr110618] Date: Thu, 13 Jul 2023 10:52:36 +0200 Message-Id: <20230713085236.330222-15-mikael@gcc.gnu.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230713085236.330222-1-mikael@gcc.gnu.org> References: <20230713085236.330222-1-mikael@gcc.gnu.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mikael Morin via Gcc-patches From: Mikael Morin Reply-To: Mikael Morin Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771295786549584701 X-GMAIL-MSGID: 1771295786549584701 Pass already evaluated class container argument from gfc_conv_procedure_call down to gfc_add_finalizer_call through gfc_deallocate_scalar_with_status and gfc_deallocate_with_status, to avoid repeatedly evaluating the same data reference expressions in the generated code. PR fortran/110618 gcc/fortran/ChangeLog: * trans.h (gfc_deallocate_with_status): Add class container argument. (gfc_deallocate_scalar_with_status): Ditto. * trans.cc (gfc_deallocate_with_status): Add class container argument and pass it down to gfc_add_finalize_call. (gfc_deallocate_scalar_with_status): Same. * trans-array.cc (structure_alloc_comps): Update caller. * trans-stmt.cc (gfc_trans_deallocate): Ditto. * trans-expr.cc (gfc_conv_procedure_call): Ditto. Pass pre-evaluated class container argument if it's available. gcc/testsuite/ChangeLog: * gfortran.dg/intent_out_22.f90: New test. --- gcc/fortran/trans-array.cc | 2 +- gcc/fortran/trans-expr.cc | 7 ++-- gcc/fortran/trans-stmt.cc | 3 +- gcc/fortran/trans.cc | 11 +++--- gcc/fortran/trans.h | 7 ++-- gcc/testsuite/gfortran.dg/intent_out_22.f90 | 37 +++++++++++++++++++++ 6 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/intent_out_22.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 1c2af55d436..951cecfa5d5 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -9472,7 +9472,7 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest, tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, true, - NULL, caf_dereg_mode, + NULL, caf_dereg_mode, NULL_TREE, add_when_allocated, caf_token); gfc_add_expr_to_block (&tmpblock, tmp); diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index dbb04f8c434..8258543b456 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -6706,9 +6706,10 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, if (e->ts.type == BT_CLASS) ptr = gfc_class_data_get (ptr); + tree cls = parmse.class_container; tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE, NULL_TREE, true, - e, e->ts); + e, e->ts, cls); gfc_add_expr_to_block (&block, tmp); tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ptr, @@ -6900,10 +6901,12 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, ptr = parmse.expr; ptr = gfc_class_data_get (ptr); + tree cls = parmse.class_container; tmp = gfc_deallocate_with_status (ptr, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, true, e, - GFC_CAF_COARRAY_NOCOARRAY); + GFC_CAF_COARRAY_NOCOARRAY, + cls); gfc_add_expr_to_block (&block, tmp); tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ptr, diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index 7e768343a57..93f36bfb955 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -7462,7 +7462,8 @@ gfc_trans_deallocate (gfc_code *code) { tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish, false, al->expr, - al->expr->ts, is_coarray); + al->expr->ts, NULL_TREE, + is_coarray); gfc_add_expr_to_block (&se.pre, tmp); /* Set to zero after deallocation. */ diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc index 18965b9cbd2..569fad45031 100644 --- a/gcc/fortran/trans.cc +++ b/gcc/fortran/trans.cc @@ -1777,8 +1777,8 @@ tree gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, tree errlen, tree label_finish, bool can_fail, gfc_expr* expr, - int coarray_dealloc_mode, tree add_when_allocated, - tree caf_token) + int coarray_dealloc_mode, tree class_container, + tree add_when_allocated, tree caf_token) { stmtblock_t null, non_null; tree cond, tmp, error; @@ -1872,7 +1872,7 @@ gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, gfc_start_block (&non_null); if (add_when_allocated) gfc_add_expr_to_block (&non_null, add_when_allocated); - gfc_add_finalizer_call (&non_null, expr); + gfc_add_finalizer_call (&non_null, expr, class_container); if (coarray_dealloc_mode == GFC_CAF_COARRAY_NOCOARRAY || flag_coarray != GFC_FCOARRAY_LIB) { @@ -1977,7 +1977,8 @@ gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, tree gfc_deallocate_scalar_with_status (tree pointer, tree status, tree label_finish, bool can_fail, gfc_expr* expr, - gfc_typespec ts, bool coarray) + gfc_typespec ts, tree class_container, + bool coarray) { stmtblock_t null, non_null; tree cond, tmp, error; @@ -2030,7 +2031,7 @@ gfc_deallocate_scalar_with_status (tree pointer, tree status, tree label_finish, gfc_start_block (&non_null); /* Free allocatable components. */ - finalizable = gfc_add_finalizer_call (&non_null, expr); + finalizable = gfc_add_finalizer_call (&non_null, expr, class_container); if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp) { int caf_mode = coarray diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h index be9ccbc3d29..109d7647235 100644 --- a/gcc/fortran/trans.h +++ b/gcc/fortran/trans.h @@ -771,10 +771,11 @@ void gfc_allocate_using_malloc (stmtblock_t *, tree, tree, tree); /* Generate code to deallocate an array. */ tree gfc_deallocate_with_status (tree, tree, tree, tree, tree, bool, - gfc_expr *, int, tree a = NULL_TREE, - tree c = NULL_TREE); + gfc_expr *, int, tree = NULL_TREE, + tree a = NULL_TREE, tree c = NULL_TREE); tree gfc_deallocate_scalar_with_status (tree, tree, tree, bool, gfc_expr*, - gfc_typespec, bool c = false); + gfc_typespec, tree = NULL_TREE, + bool c = false); /* Generate code to call realloc(). */ tree gfc_call_realloc (stmtblock_t *, tree, tree); diff --git a/gcc/testsuite/gfortran.dg/intent_out_22.f90 b/gcc/testsuite/gfortran.dg/intent_out_22.f90 new file mode 100644 index 00000000000..a38afccf0e5 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/intent_out_22.f90 @@ -0,0 +1,37 @@ +! { dg-do run } +! +! PR fortran/110618 +! Check that if a data reference is passed as actual argument whose dummy +! has INTENT(OUT) attribute, any other argument depending on the +! same data reference is evaluated before the data reference deallocation. + +program p + implicit none + type t + integer :: i + end type t + type u + class(t), allocatable :: ta(:) + end type u + type(u), allocatable :: c(:) + class(t), allocatable :: d(:) + allocate(c, source = [u([t(1), t(3)]), u([t(4), t(9)])]) + allocate(d, source = [t(1), t(5)]) + call bar ( & + allocated(c(d(1)%i)%ta), & + d, & + c(d(1)%i)%ta, & + allocated (c(d(1)%i)%ta) & + ) + if (allocated (c(1)%ta)) stop 11 + if (.not. allocated (c(2)%ta)) stop 11 +contains + subroutine bar (alloc, x, y, alloc2) + logical :: alloc, alloc2 + class(t), allocatable, intent(out) :: x(:) + class(t), allocatable, intent(out) :: y(:) + if (allocated (x)) stop 1 + if (.not. alloc) stop 2 + if (.not. alloc2) stop 3 + end subroutine bar +end