From patchwork Wed Jul 31 20:07:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 1967290 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=OmzYEIW8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WZ38d0Jgvz1ybb for ; Thu, 1 Aug 2024 06:09:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D38A385B503 for ; Wed, 31 Jul 2024 20:09:19 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp.smtpout.orange.fr (smtp-30.smtpout.orange.fr [80.12.242.30]) by sourceware.org (Postfix) with ESMTPS id 28CA93858289; Wed, 31 Jul 2024 20:07:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 28CA93858289 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 28CA93858289 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=80.12.242.30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456467; cv=none; b=kamQWSzT44dqv838SgL6IR41XIpkkZIzUALqeKJXepGpb4cq35TH8l0d2nhiS96oQ051xvSztYyQ64deeh5qpCGyrc1eF9DYVaqiDqZKdC1XuhMnnoIDlsgkGGGsy0ykQ/zmFcEQjVPRerr6aZXfzkprAxGrpO6mCmiZge54C5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456467; c=relaxed/simple; bh=aJJC+LZha53NiYplU3HDCr26ngJxJ8wuVYmmgMeOn6E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=M74RJqTnJmJMUVzY4Qd10fRlUC4KjwWRCj/p30lDBupEmjINwlyqs/Xt8lFG+RCeGViiOkUvyH7V/DjBwlh+Wz0k5YArIJwwY84qMMGBZxYcxmaR8UQtbOzUC90/J+sFJ3WAESkTq+41o41XtxfVS65f+vGRnBLvnoudSVWn0wM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from cyrano.home ([86.215.161.51]) by smtp.orange.fr with ESMTPA id ZFbisBHiIYjQzZFbrsqYRR; Wed, 31 Jul 2024 22:07:43 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=orange.fr; s=t20230301; t=1722456463; bh=NVVowaN3yy8XjRY0cDamWllcwocAKk3I4KzdX0d+gGQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=OmzYEIW86FIKvoFijElTnpEsBU7mCW2mOZiMHPSK4qUiawrzAHCkqh4g2OUTngr2b IwUUOD7WNEkPvuMkvwL0G1HrseGUJWJytmuSgBqXLPEbev50aGWVuGUejXXoSoyR0m xOhJ56mdBmPUSn5I47/CV6GKQrP44oATKiWdqE7yYdyMsvepEFljDYhUZAQrdXuY8l 1nMMfsenYo/Gzp0BPFsLiBfBY9tos8Q42H/F6xG7tghXL5GPNdkzOHTLLir4B9q1Yb MHSOddezVOuim3YOiCXdXjBD5vqU3wtCXQR87yvzdJrkIUVdDcGpHiCanfmgViu31X c0te5F0BpgHHQ== X-ME-Helo: cyrano.home X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Wed, 31 Jul 2024 22:07:43 +0200 X-ME-IP: 86.215.161.51 From: Mikael Morin To: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org Subject: [PATCH 3/8] fortran: Inline MINLOC/MAXLOC with no DIM and ARRAY of rank 1 [PR90608] Date: Wed, 31 Jul 2024 22:07:30 +0200 Message-ID: <20240731200735.229898-4-morin-mikael@orange.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240731200735.229898-1-morin-mikael@orange.fr> References: <20240731200735.229898-1-morin-mikael@orange.fr> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 Regression-tested on x86_64-pc-linux-gnu. OK for master? -- >8 -- Enable inline code generation for the MINLOC and MAXLOC intrinsic, if the DIM argument is not present and ARRAY has rank 1. This case is similar to the case where the result is scalar (DIM present and rank 1 ARRAY), which already supports inline expansion of the intrinsic. Both cases return the same value, with the difference that the result is an array of size 1 if DIM is absent, whereas it's a scalar if DIM is present. So all there is to do for the new case to work is hook the inline expansion with the scalarizer. PR fortran/90608 gcc/fortran/ChangeLog: * trans-array.cc (gfc_conv_ss_startstride): Set the scalarization rank based on the MINLOC/MAXLOC rank if needed. Call the inline code generation and setup the scalarizer array descriptor info in the MINLOC and MAXLOC cases. * trans-intrinsic.cc (gfc_conv_intrinsic_minmaxloc): Return the result array element if the scalarizer is setup and we are inside the loops. Restrict library function call dispatch to the case where inline expansion is not supported. Declare an array result if the expression isn't scalar. Initialize the array result single element and return the result variable if the expression isn't scalar. (walk_inline_intrinsic_minmaxloc): New function. (walk_inline_intrinsic_function): Add MINLOC and MAXLOC cases, dispatching to walk_inline_intrinsic_minmaxloc. (gfc_add_intrinsic_ss_code): Add MINLOC and MAXLOC cases. (gfc_inline_intrinsic_function_p): Return true if ARRAY has rank 1, regardless of DIM. --- gcc/fortran/trans-array.cc | 25 +++++ gcc/fortran/trans-intrinsic.cc | 198 ++++++++++++++++++++++----------- 2 files changed, 155 insertions(+), 68 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index c93a5f1e754..0c78e1fecd8 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -4771,6 +4771,8 @@ gfc_conv_ss_startstride (gfc_loopinfo * loop) case GFC_ISYM_UBOUND: case GFC_ISYM_LCOBOUND: case GFC_ISYM_UCOBOUND: + case GFC_ISYM_MAXLOC: + case GFC_ISYM_MINLOC: case GFC_ISYM_SHAPE: case GFC_ISYM_THIS_IMAGE: loop->dimen = ss->dimen; @@ -4820,6 +4822,29 @@ done: case GFC_SS_INTRINSIC: switch (expr->value.function.isym->id) { + case GFC_ISYM_MINLOC: + case GFC_ISYM_MAXLOC: + { + gfc_se se; + gfc_init_se (&se, nullptr); + se.loop = loop; + se.ss = ss; + gfc_conv_intrinsic_function (&se, expr); + gfc_add_block_to_block (&outer_loop->pre, &se.pre); + gfc_add_block_to_block (&outer_loop->post, &se.post); + + info->descriptor = se.expr; + + info->data = gfc_conv_array_data (info->descriptor); + info->data = gfc_evaluate_now (info->data, &outer_loop->pre); + + info->offset = gfc_index_zero_node; + info->start[0] = gfc_index_zero_node; + info->end[0] = gfc_index_zero_node; + info->stride[0] = gfc_index_one_node; + continue; + } + /* Fall through to supply start and stride. */ case GFC_ISYM_LBOUND: case GFC_ISYM_UBOUND: diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc index cc0d00f4e39..a947dd1ba0b 100644 --- a/gcc/fortran/trans-intrinsic.cc +++ b/gcc/fortran/trans-intrinsic.cc @@ -5273,66 +5273,69 @@ strip_kind_from_actual (gfc_actual_arglist * actual) we need to handle. For performance reasons we sometimes create two loops instead of one, where the second one is much simpler. Examples for minloc intrinsic: - 1) Result is an array, a call is generated - 2) Array mask is used and NaNs need to be supported: - limit = Infinity; - pos = 0; - S = from; - while (S <= to) { - if (mask[S]) { - if (pos == 0) pos = S + (1 - from); - if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; } - } - S++; - } - goto lab2; - lab1:; - while (S <= to) { - if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } - S++; - } - lab2:; - 3) NaNs need to be supported, but it is known at compile time or cheaply - at runtime whether array is nonempty or not: - limit = Infinity; - pos = 0; - S = from; - while (S <= to) { - if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; } - S++; - } - if (from <= to) pos = 1; - goto lab2; - lab1:; - while (S <= to) { - if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } - S++; - } - lab2:; - 4) NaNs aren't supported, array mask is used: - limit = infinities_supported ? Infinity : huge (limit); - pos = 0; - S = from; - while (S <= to) { - if (mask[S]) { limit = a[S]; pos = S + (1 - from); goto lab1; } - S++; - } - goto lab2; - lab1:; - while (S <= to) { - if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } - S++; - } - lab2:; - 5) Same without array mask: - limit = infinities_supported ? Infinity : huge (limit); - pos = (from <= to) ? 1 : 0; - S = from; - while (S <= to) { - if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } - S++; - } - For 3) and 5), if mask is scalar, this all goes into a conditional, + A: Result is scalar. + 1) Array mask is used and NaNs need to be supported: + limit = Infinity; + pos = 0; + S = from; + while (S <= to) { + if (mask[S]) { + if (pos == 0) pos = S + (1 - from); + if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; } + } + S++; + } + goto lab2; + lab1:; + while (S <= to) { + if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } + S++; + } + lab2:; + 2) NaNs need to be supported, but it is known at compile time or cheaply + at runtime whether array is nonempty or not: + limit = Infinity; + pos = 0; + S = from; + while (S <= to) { + if (a[S] <= limit) { limit = a[S]; pos = S + (1 - from); goto lab1; } + S++; + } + if (from <= to) pos = 1; + goto lab2; + lab1:; + while (S <= to) { + if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } + S++; + } + lab2:; + 3) NaNs aren't supported, array mask is used: + limit = infinities_supported ? Infinity : huge (limit); + pos = 0; + S = from; + while (S <= to) { + if (mask[S]) { limit = a[S]; pos = S + (1 - from); goto lab1; } + S++; + } + goto lab2; + lab1:; + while (S <= to) { + if (mask[S]) if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } + S++; + } + lab2:; + 4) Same without array mask: + limit = infinities_supported ? Infinity : huge (limit); + pos = (from <= to) ? 1 : 0; + S = from; + while (S <= to) { + if (a[S] < limit) { limit = a[S]; pos = S + (1 - from); } + S++; + } + B) ARRAY has rank 1, and DIM is absent. Use the same code as the scalar + case and wrap the result in an array. + C) Otherwise, a call is generated + For 2) and 4), if mask is scalar, this all goes into a conditional, setting pos = 0; in the else branch. Since we now also support the BACK argument, instead of using @@ -5346,7 +5349,7 @@ strip_kind_from_actual (gfc_actual_arglist * actual) .... The optimizer is smart enough to move the condition out of the loop. - The are now marked as unlikely to for further speedup. */ + They are now marked as unlikely too for further speedup. */ static void gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) @@ -5377,6 +5380,7 @@ gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) gfc_expr *backexpr; gfc_se backse; tree pos; + tree result_var = NULL_TREE; int n; bool optional_mask; @@ -5392,8 +5396,19 @@ gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) if (se->ss) { - gfc_conv_intrinsic_funcall (se, expr); - return; + if (se->ss->info->useflags) + { + /* The inline implementation of MINLOC/MAXLOC has been generated + before, out of the scalarization loop; now we can just use the + result. */ + gfc_conv_tmp_array_ref (se); + return; + } + else if (!gfc_inline_intrinsic_function_p (expr)) + { + gfc_conv_intrinsic_funcall (se, expr); + return; + } } arrayexpr = actual->expr; @@ -5419,10 +5434,29 @@ gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) return; } + type = gfc_typenode_for_spec (&expr->ts); + + if (expr->rank > 0) + { + gfc_array_spec as; + memset (&as, 0, sizeof (as)); + + as.rank = 1; + as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind, + &arrayexpr->where, + HOST_WIDE_INT_1); + as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind, + &arrayexpr->where, + HOST_WIDE_INT_1); + + tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true); + + result_var = gfc_create_var (array, "loc_result"); + } + /* Initialize the result. */ pos = gfc_create_var (gfc_array_index_type, "pos"); offset = gfc_create_var (gfc_array_index_type, "offset"); - type = gfc_typenode_for_spec (&expr->ts); /* Walk the arguments. */ arrayss = gfc_walk_expr (arrayexpr); @@ -5826,7 +5860,18 @@ gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op) } gfc_cleanup_loop (&loop); - se->expr = convert (type, pos); + tree value = convert (type, pos); + if (expr->rank > 0) + { + tree res_arr_ref = gfc_build_array_ref (result_var, gfc_index_zero_node, + NULL_TREE, true); + + gfc_add_modify (&se->pre, res_arr_ref, value); + + se->expr = result_var; + } + else + se->expr = value; } /* Emit code for findloc. */ @@ -11535,6 +11580,19 @@ walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr) } +/* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the + function is to be inlined. */ + +static gfc_ss * +walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED) +{ + if (expr->rank == 0) + return ss; + + return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC); +} + + static gfc_ss * walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr) { @@ -11548,6 +11606,10 @@ walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr) case GFC_ISYM_TRANSPOSE: return walk_inline_intrinsic_transpose (ss, expr); + case GFC_ISYM_MAXLOC: + case GFC_ISYM_MINLOC: + return walk_inline_intrinsic_minmaxloc (ss, expr); + default: gcc_unreachable (); } @@ -11567,6 +11629,8 @@ gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss) case GFC_ISYM_LBOUND: case GFC_ISYM_UCOBOUND: case GFC_ISYM_LCOBOUND: + case GFC_ISYM_MAXLOC: + case GFC_ISYM_MINLOC: case GFC_ISYM_THIS_IMAGE: case GFC_ISYM_SHAPE: break; @@ -11658,16 +11722,14 @@ gfc_inline_intrinsic_function_p (gfc_expr *expr) return false; gfc_actual_arglist *array_arg = expr->value.function.actual; - gfc_actual_arglist *dim_arg = array_arg->next; gfc_expr *array = array_arg->expr; - gfc_expr *dim = dim_arg->expr; if (!(array->ts.type == BT_INTEGER || array->ts.type == BT_REAL)) return false; - if (array->rank == 1 && dim != nullptr) + if (array->rank == 1) return true; return false;