From patchwork Mon Oct 14 15:08:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 1997025 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=orange.fr header.i=@orange.fr header.a=rsa-sha256 header.s=t20230301 header.b=lfA7FjtF; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XS10N2NjHz1xvt for ; Tue, 15 Oct 2024 02:11:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F5F0385AE79 for ; Mon, 14 Oct 2024 15:11:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from msa.smtpout.orange.fr (smtp-74.smtpout.orange.fr [80.12.242.74]) by sourceware.org (Postfix) with ESMTPS id 12023385AE41; Mon, 14 Oct 2024 15:08:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 12023385AE41 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=orange.fr Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=orange.fr ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 12023385AE41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=80.12.242.74 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728918530; cv=none; b=MMl2ca+ZcPIVYH7D1xe97C1FlLx9BLEystFSMgGb+wfWNTEEo57kNbW8uCcpoLpKxJLOPdKgLf/Eu9lEGL4iMZ5jWjNHKoVxaIZD6LGBMDf6INgZ/a6rgFYFGQWLUbbr1nmGEMlRIh6PFZb6uAREB9tPoFtjdxEV8FdRFPGlxa0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728918530; c=relaxed/simple; bh=iwXDMlqVxFa47yNQ/IMbyLbVGUiQKr3mtZjwwi8q9XQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PuYZlFgEEOLwC/21co7UPhaxe6WS7v4iluC+MuafVkHHjy5InJWrTdakZ8jwGjiJ4m2+F/fNF5nryLlcLlMzsA29UCTbqwqVtW1///A1VN5x2Zvg0Ma1diIq1BBZWiHjU250GsLmc474FWeN8senmhYkbmro7UBYmdUdLLoqfJk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from cyrano.home ([86.215.161.51]) by smtp.orange.fr with ESMTPA id 0MgBtHlyUIPG80MgQtVhLw; Mon, 14 Oct 2024 17:08:30 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=orange.fr; s=t20230301; t=1728918510; bh=vtf5lhqJfcpkxFIrtMhJemFoiZgaP/Q5cOe+O1fI4ik=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=lfA7FjtFWs5KK+coW++E8CrFV54BB4kkZST8ggEBfGZHppUnk3yzDB5RO4txbJaFo 2CUwYK3Bwy/nck1URMefssG1hPNajnFDotj/ktRqI+M4pqD/04339hkZLzmm262dW9 dE+bdldWf2NiphPaSSohVV2dTKVrGqBArCqYoo9hntkEt08XjnYADPYKPFcYVoPuEP 4Dovi+jU9I4lBPa5dyYDNPDOkk07WwwE+UIUzAjT2xsxyCtujRQa2pSYQqjUFMX/NJ LLCAxfF8y5NQE1p5BnS5sXvsGnej6Ygu0LGoFoavIeDqw9bx8eMJhF3z7Lsf96iXwR D5oFw6j7FAifg== X-ME-Helo: cyrano.home X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Mon, 14 Oct 2024 17:08:30 +0200 X-ME-IP: 86.215.161.51 From: Mikael Morin To: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org Subject: [PATCH 6/7] fortran: Check for empty MINLOC/MAXLOC ARRAY along DIM only Date: Mon, 14 Oct 2024 17:08:15 +0200 Message-ID: <20241014150816.315478-7-morin-mikael@orange.fr> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241014150816.315478-1-morin-mikael@orange.fr> References: <20241014150816.315478-1-morin-mikael@orange.fr> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: Mikael Morin Bootstrapped and regression-tested on x86_64-pc-linux-gnu. OK for master? -- >8 -- In the function generating inline code to implement MINLOC and MAXLOC, only check for ARRAY size along DIM if DIM is present. The check for ARRAY emptyness had been checking the size of the full array, which is correct for MINLOC and MAXLOC without DIM. But if DIM is present, the reduction is along DIM only so the check for emptyness should consider that dimension only as well. This sounds like a correctness issue, but fortunately the cases where it makes a difference are cases where ARRAY is empty, so even if the value calculated for MINLOC or MAXLOC is wrong, it's wrapped in a zero iteration loop, and the wrong values are not actually used. In the end this just avoids unnecessary calculations. A previous version of this patch regressed on non-constant DIM with rank 1 ARRAY. The new testcase checks that that case is supported. gcc/fortran/ChangeLog: * trans-intrinsic.cc (gfc_conv_intrinsic_minmaxloc): Only get the size along DIM instead of the full size if DIM is present. gcc/testsuite/ChangeLog: * gfortran.dg/minmaxloc_22.f90: New test. --- gcc/fortran/trans-intrinsic.cc | 19 +++++++++++++++- gcc/testsuite/gfortran.dg/minmaxloc_22.f90 | 26 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/minmaxloc_22.f90 diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc index b77183ab11e..e1a5fdef26c 100644 --- a/gcc/fortran/trans-intrinsic.cc +++ b/gcc/fortran/trans-intrinsic.cc @@ -5641,7 +5641,24 @@ gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) if (!(maskexpr && maskexpr->rank > 0)) { mpz_t asize; - if (gfc_array_size (arrayexpr, &asize)) + bool reduction_size_known; + + if (dim_present) + { + int reduction_dim; + if (dim_arg->expr->expr_type == EXPR_CONSTANT) + reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1; + else if (arrayexpr->rank == 1) + reduction_dim = 0; + else + gcc_unreachable (); + reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim, + &asize); + } + else + reduction_size_known = gfc_array_size (arrayexpr, &asize); + + if (reduction_size_known) { nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind); mpz_clear (asize); diff --git a/gcc/testsuite/gfortran.dg/minmaxloc_22.f90 b/gcc/testsuite/gfortran.dg/minmaxloc_22.f90 new file mode 100644 index 00000000000..ec97d1435af --- /dev/null +++ b/gcc/testsuite/gfortran.dg/minmaxloc_22.f90 @@ -0,0 +1,26 @@ +! { dg-do compile } +! { dg-additional-options "-O" } +! +! Check that the inline code generated for MINLOC and MAXLOC supports +! a non-constant DIM argument if ARRAY has rank 1. + +program p + implicit none + integer, parameter :: n = 5 + integer :: a(n), i + a = (/ (i**2, i=1,n) /) + print *, f(a, 1), g(a, 1) +contains + function f(a, d) + integer :: a(n) + integer :: d + integer :: f + f = minloc(a, dim=d) + end function + function g(a, d) + integer :: a(n) + integer :: d + integer :: g + g = maxloc(a, dim=d) + end function +end program p