From patchwork Wed Oct 23 18:12:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Kargl X-Patchwork-Id: 1182346 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-511616-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=troutmask.apl.washington.edu Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="vDk9nQ4M"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46yz4F16STz9sPF for ; Thu, 24 Oct 2019 05:12:28 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:reply-to:mime-version:content-type; q=dns; s=default; b=QbIR+2hfsWZ5UIaXo4x9/Ar/yGjOXnDYwNkj8IOKxtq Ks5sOfymnL+ei31owlkX9UUSGDLvOstZw15+kQ0k5iBumKEKWpgMsHkhFGfWQjSc SJE/aVGvzOzebZa1q5mMuQAr9xqaXnL4yCQU2ayvr6F3ZmqRV5zy/tG+B7o+vDTk = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:reply-to:mime-version:content-type; s=default; bh=v26OD9VpQZHVCDVuODGJuy1trtE=; b=vDk9nQ4M7y57bPco/ N4htlSTFhCBOo1KlMQLw6vbBctIkiWYq9LdZlCiuMZuFI2IDk3CRy4lZ9OE2d2AC Av/18sNtttmpGG6uAs9dFX3aR34CcPTkEm9lnxtxCqg1x78foFmEQTezs0K8w2YE giZt/zm8vj8/6rveulph3R5KTU= Received: (qmail 101893 invoked by alias); 23 Oct 2019 18:12:22 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 101877 invoked by uid 89); 23 Oct 2019 18:12:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS autolearn=ham version=3.3.1 spammy=void_type_node X-HELO: troutmask.apl.washington.edu Received: from troutmask.apl.washington.edu (HELO troutmask.apl.washington.edu) (128.95.76.21) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 23 Oct 2019 18:12:20 +0000 Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.15.2/8.15.2) with ESMTPS id x9NICJrj062484 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 23 Oct 2019 11:12:19 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.15.2/8.15.2/Submit) id x9NICIa7062483; Wed, 23 Oct 2019 11:12:18 -0700 (PDT) (envelope-from sgk) Date: Wed, 23 Oct 2019 11:12:18 -0700 From: Steve Kargl To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR fortran/92178 -- Re-order argument deallocation Message-ID: <20191023181218.GA62452@troutmask.apl.washington.edu> Reply-To: sgk@troutmask.apl.washington.edu MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.12.2 (2019-09-21) The attached patch has been tested on x86_64-*-freebsd. OK to commit? 2019-10-23 Steven G. Kargl PR fortran/92178 * trans-expr.c (gfc_conv_procedure_call): Evaluate args and then deallocate actual args assocated with intent(out) dummies. 2019-10-23 Steven G. Kargl PR fortran/92178 * gfortran.dg/pr92178.f90: New test. Note, in gfc_conv_procedure_call() there are 3 blocks of code that deal with the deallocation of actual arguments assocated with intent(out) dummy arguments. The patch affects the first and third blocks. The 2nd block, lines 6071-6111, concerns CLASS and finalization. I use neither, so have no idea what Fortran requires. More importantly, I have very little understanding of gfortran's internal implementation for CLASS and finalization. Someone who cares about CLASS and finalization will need to consider how to possibly fix a possible issue. Index: gcc/fortran/trans-expr.c =================================================================== --- gcc/fortran/trans-expr.c (revision 277296) +++ gcc/fortran/trans-expr.c (working copy) @@ -5405,6 +5405,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym gfc_component *comp = NULL; int arglen; unsigned int argc; + stmtblock_t dealloc_blk; + bool saw_dealloc = false; arglist = NULL; retargs = NULL; @@ -5445,6 +5447,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym info = NULL; gfc_init_block (&post); + gfc_init_block (&dealloc_blk); gfc_init_interface_mapping (&mapping); if (!comp) { @@ -5976,8 +5979,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); + saw_dealloc = true; + gfc_add_expr_to_block (&dealloc_blk, tmp); } if (fsym && (fsym->ts.type == BT_DERIVED @@ -6265,7 +6268,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym void_type_node, gfc_conv_expr_present (e->symtree->n.sym), tmp, build_empty_stmt (input_location)); - gfc_add_expr_to_block (&se->pre, tmp); + saw_dealloc = true; + gfc_add_expr_to_block (&dealloc_blk, tmp); } } } @@ -6636,6 +6640,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym vec_safe_push (arglist, parmse.expr); } + if (saw_dealloc) + gfc_add_block_to_block (&se->pre, &dealloc_blk); gfc_finish_interface_mapping (&mapping, &se->pre, &se->post); if (comp) Index: gcc/testsuite/gfortran.dg/pr92178.f90 =================================================================== --- gcc/testsuite/gfortran.dg/pr92178.f90 (nonexistent) +++ gcc/testsuite/gfortran.dg/pr92178.f90 (working copy) @@ -0,0 +1,22 @@ +! { dg-do run } +! Original code contributed by Vladimir Fuka +! PR fortran/92178 +program foo + + implicit none + + integer, allocatable :: a(:) + + allocate(a, source=[1]) + + call assign(a, (a(1))) + + if (allocated(a) .neqv. .false.) stop 1 + + contains + subroutine assign(a, b) + integer, allocatable, intent(out) :: a(:) + integer :: b + if (b /= 1) stop 2 + end subroutine +end program