From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from cc-smtpout2.netcologne.de (cc-smtpout2.netcologne.de [IPv6:2001:4dd0:100:1062:25:2:0:2]) by sourceware.org (Postfix) with ESMTPS id 58DE9386EC6C for ; Fri, 19 Mar 2021 06:36:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 58DE9386EC6C Received: from cc-smtpin3.netcologne.de (cc-smtpin3.netcologne.de [89.1.8.203]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id 875DE12598; Fri, 19 Mar 2021 07:36:27 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin3.netcologne.de (Postfix) with ESMTP id 792AC11E55; Fri, 19 Mar 2021 07:36:27 +0100 (CET) Received: from [2001:4dd6:22d3:0:d9ee:f94a:6429:461d] (helo=cc-smtpin3.netcologne.de) by localhost with ESMTP (eXpurgate 4.19.0) (envelope-from ) id 6054466b-4ebb-7f0000012729-7f000001e5b4-1 for ; Fri, 19 Mar 2021 07:36:27 +0100 Received: from linux-p51k.fritz.box (2001-4dd6-22d3-0-d9ee-f94a-6429-461d.ipv6dyn.netcologne.de [IPv6:2001:4dd6:22d3:0:d9ee:f94a:6429:461d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by cc-smtpin3.netcologne.de (Postfix) with ESMTPSA; Fri, 19 Mar 2021 07:36:23 +0100 (CET) Subject: Re: MATMUL broken with frontend optimization. From: Thomas Koenig To: Steve Kargl Cc: Richard Biener , Tobias Burnus , "fortran@gcc.gnu.org" References: <20210318074849.GA22541@troutmask.apl.washington.edu> <563cee48-fbcc-09bc-0cd1-f05082e4feb3@codesourcery.com> <20210318161347.GA24201@troutmask.apl.washington.edu> <1780c473-3523-316f-c372-52824d062a01@netcologne.de> <20210318202239.GA25584@troutmask.apl.washington.edu> <367461e9-72b4-0f8d-d04e-878789595942@netcologne.de> <20210318220758.GA26001@troutmask.apl.washington.edu> <34f8dfc0-c752-c8fb-8f6b-6880b984e69c@netcologne.de> Message-ID: <3e9a3047-dae1-49c8-fc31-507579c76bb3@netcologne.de> Date: Fri, 19 Mar 2021 07:36:22 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <34f8dfc0-c752-c8fb-8f6b-6880b984e69c@netcologne.de> Content-Type: multipart/mixed; boundary="------------C231EF74837A8D3955B76C4A" Content-Language: de-DE X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Mar 2021 06:36:30 -0000 This is a multi-part message in MIME format. --------------C231EF74837A8D3955B76C4A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Am 19.03.21 um 07:19 schrieb Thomas Koenig: > I'll work on a patch. So, here's a concept patch. It still needs a ChangeLog and testsuite adjustment, but this is what I would propose to use. Regards Thomas --------------C231EF74837A8D3955B76C4A Content-Type: text/x-patch; charset=UTF-8; name="p1.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="p1.diff" diff --git a/gcc/fortran/frontend-passes.c b/gcc/fortran/frontend-passes.c index cfc47471cf1..7d3eae67632 100644 --- a/gcc/fortran/frontend-passes.c +++ b/gcc/fortran/frontend-passes.c @@ -3307,7 +3307,7 @@ get_operand (gfc_intrinsic_op op, gfc_expr *e1, gfc_expr *e2) removed by DCE. Only called for rank-two matrices A and B. */ static gfc_code * -inline_limit_check (gfc_expr *a, gfc_expr *b, int limit) +inline_limit_check (gfc_expr *a, gfc_expr *b, int limit, int rank_a) { gfc_expr *inline_limit; gfc_code *if_1, *if_2, *else_2; @@ -3315,16 +3315,28 @@ inline_limit_check (gfc_expr *a, gfc_expr *b, int limit) gfc_typespec ts; gfc_expr *cond; + gcc_assert (rank_a == 1 || rank_a == 2); + /* Calculation is done in real to avoid integer overflow. */ inline_limit = gfc_get_constant_expr (BT_REAL, gfc_default_real_kind, &a->where); mpfr_set_si (inline_limit->value.real, limit, GFC_RND_MODE); - mpfr_pow_ui (inline_limit->value.real, inline_limit->value.real, 3, + + /* Set the limit according to the rank. */ + mpfr_pow_ui (inline_limit->value.real, inline_limit->value.real, rank_a + 1, GFC_RND_MODE); a1 = get_array_inq_function (GFC_ISYM_SIZE, a, 1); - a2 = get_array_inq_function (GFC_ISYM_SIZE, a, 2); + + /* For a_rank = 1, must use one as the size of a along the second + dimension as to avoid too much code duplication. */ + + if (rank_a == 2) + a2 = get_array_inq_function (GFC_ISYM_SIZE, a, 2); + else + a2 = gfc_get_int_expr (gfc_index_integer_kind, &a->where, 1); + b2 = get_array_inq_function (GFC_ISYM_SIZE, b, 2); gfc_clear_ts (&ts); @@ -4243,11 +4255,13 @@ inline_matmul_assign (gfc_code **c, int *walk_subtrees, /* Take care of the inline flag. If the limit check evaluates to a constant, dead code elimination will eliminate the unneeded branch. */ - if (flag_inline_matmul_limit > 0 && matrix_a->rank == 2 + if (flag_inline_matmul_limit > 0 + && (matrix_a->rank == 1 || matrix_a->rank == 2) && matrix_b->rank == 2) { if_limit = inline_limit_check (matrix_a, matrix_b, - flag_inline_matmul_limit); + flag_inline_matmul_limit, + matrix_a->rank); /* Insert the original statement into the else branch. */ if_limit->block->block->next = co; @@ -4757,7 +4771,7 @@ call_external_blas (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, return 0; /* Generate the if statement and hang it into the tree. */ - if_limit = inline_limit_check (matrix_a, matrix_b, flag_blas_matmul_limit); + if_limit = inline_limit_check (matrix_a, matrix_b, flag_blas_matmul_limit, 2); co_next = co->next; (*current_code) = if_limit; co->next = NULL; --------------C231EF74837A8D3955B76C4A--