From patchwork Wed Jan 10 22:24:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 187099 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1088238dyi; Wed, 10 Jan 2024 14:25:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3HaL9MTMm8/WJa0g590Pd49NRFYrhaYXp5zcp3b3QpF4wTgSHfb4kevvARM+5FU19ekAA X-Received: by 2002:a05:622a:1909:b0:429:bc2e:9ca4 with SMTP id w9-20020a05622a190900b00429bc2e9ca4mr268627qtc.67.1704925551571; Wed, 10 Jan 2024 14:25:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704925551; cv=pass; d=google.com; s=arc-20160816; b=QjQBp4J2axS+MarAXndIElirl7sEE+NPUS9IGrHZPvKv2K70B0dvwo82MT6O8uJBX5 vFshEmez8vHfAFvirjNy7vrKXPD2FREdsB8zI+oK6/7FnwHFwaDTMbjKcH97tAIM2Jf5 INIRsTWZxZ3eXJn2Ibuc2jPLW5yLrcNfiJdr3q8U4BWibdon9QEhKFrqWP5StmOpd8FX KUUCDaY60Ww4dNkUyMmvnfyDNIsf8g6UkzWt2QS+kJM9/kJNz5MIzu+c/Mm4ATNdGFdP UStw0e3UB06Nnl/GI/78s9Gr2gPysSCGVuaTBX5rTUhLUZ/Sd/IlMGAt0PuPFp/OaPXj JtkA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:ui-outboundreport:sensitivity :importance:date:subject:to:from:message-id:mime-version :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=fV3f9WVw9Uaop91kDLHqQ82wXRjMWYK2MaS+Ql2MI+E=; fh=+IEfvAe+9BRgPHWhQEl2uIBTtAiiGDh1ExRZeB5JJoc=; b=P2ehuQDAeQLzJe4m078zy9EIMWFTwVyg30fhEybGHSpulU0R2ev5hQqskzh8p5dCe7 2+9xN+bhZNJjVR0av1YzhFi6jBiFRWgc2OYz4eCOdFZZYpXUjXi/zm8Hfmxc3fD+oi7D E/mEZ2snlGFWyBKwTyX56tQSK06DKU2ZI2wbjO61y+T1EwvBLmIhpFtwt1XTLtL8zNP0 e1IvV7ekVwoQ3kiAWzUpovVMy6OLuUe/glvBsh+gwNmOdegc3oCin0htCR26p5bM7Wms b9jhFmnPIf5P+JgzwUcRzdHGYGKXtcH7jjCb84H5Ry64ePwDma95vCnGDJtZ2e3EYtfk wt6Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=gJ0P21Sx; arc=pass (i=1); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v27-20020a05622a189b00b00429974e00edsi4862306qtc.786.2024.01.10.14.25.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 14:25:51 -0800 (PST) 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=@gmx.de header.s=s31663417 header.b=gJ0P21Sx; arc=pass (i=1); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D6C6E386C59F for ; Wed, 10 Jan 2024 22:25:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by sourceware.org (Postfix) with ESMTPS id 98F3B3858407; Wed, 10 Jan 2024 22:24:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 98F3B3858407 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 98F3B3858407 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.17.21 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704925466; cv=none; b=Yf27bk/rzgutcKFyV/46np3Zw5+RkZHqW24wJJC7nLGFi6KqMJEEBmnyAYZ4q+PCJ3GKmQbpistyQ0yYLTzRbWEVslURpFaSOs5pW0MXT3akIba5yRiSolfw6UlHz4WWAd+fTyDRJTiPN+qJoX3DEXdtxoxX0DHe2P10oGjUsww= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704925466; c=relaxed/simple; bh=VNKAHeXrPTEXG19iCRnDjeTRg+z0HwSgwOHXOHDkuoc=; h=DKIM-Signature:MIME-Version:Message-ID:From:To:Subject:Date; b=K6ovPR5t0eQ7Bf3bzniePGFJj7xcR/FF07ag/dz7ZZ64LpQlwbKy3yHG1VOSRaEvna2KY1f2g9fPymv3mwc/WCZm18CiSfYbz2u8lfKNjQA1RAEDdMZxob+ZMc2XhDuRqgvs6gyodvvpth4NrgEu9Zllm+9rT6xC4yu3VSuD8+E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1704925462; x=1705530262; i=anlauf@gmx.de; bh=VNKAHeXrPTEXG19iCRnDjeTRg+z0HwSgwOHXOHDkuoc=; h=X-UI-Sender-Class:From:To:Subject:Date; b=gJ0P21Sx/jkonferlo3DMutKUT+KUwxFmpnX/Fw7gnZTWpyW0Db3gBn4YpqT1dOG CWTzTLkELqf5JYCJNhayI7C10coBpsnhO5bDo8gOu9/zKOPJrZvoZyzIe32h7At7+ qU5eGURJ1Z4fCFODGO6WSnlnLwCzWzHERIlSTCFTRscm/1zrGLJDRWGUHhAp9MufI RvcSiFUB7MDujNcHMQSDufm64hA2awtwRRU2hzfrEZo+05HJIkqaQVn4RvCYMTkjd CoOeTDUEHEFU5G1Gs1IufnlQqkOQd25txjygPF55Qo4mYXe9XXZdfWRWqtXDUNtE8 e75DWUfqDtB3EFflyQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.94.93] ([93.207.94.93]) by web-mail.gmx.net (3c-app-gmx-bap04.server.lan [172.19.172.74]) (via HTTP); Wed, 10 Jan 2024 23:24:22 +0100 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: annotations for DO CONCURRENT loops [PR113305] Date: Wed, 10 Jan 2024 23:24:22 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:BFqI1QQnXnDUp0Yeps0iRe+ttXZJ9U4RSUE/WginJ6auIkCDiAIb3oAzLByZzRaQbMvYz A0IbX1dKl27VYZFLZgXYP0Y+YlBIv4iUc47wW7q6fWFn19UCE1bkhk6CGzCkruR2HW1qniG7AcTt 51lwhs/VmcRIPX5/6UVF8rgaI7iJsjwRg8I9bYJKf1DnRfqI97D5zLuh3+mKpwF8Mt237BrXQ5/8 6msVDlfdsVgvVE0aK9doO3PNQsFaJJ4+9+4GlgEFzaG7dxPtqdnwDAN3Bgv2zLptpHJTG3kjzcTb ag= UI-OutboundReport: notjunk:1;M01:P0:aO6QmIqrJuk=;mygymBInAzDJrYq1MEKBGoAqWOV qIM8FGC1rZ+FA70geCSPukaAkavKB4RnqAVZKc5HRxRUGMJrpvmYA4SdpS1XGyKYzVkEoFLtI k9urT2lOJdHCOxnsVs8+56X3YsliHzXKbcNlWsXcPSnIa+ueqk26cRHn4WDSsQ+sP6mHbMPM7 4HA5vzZlIDjlfHITaClBKtOf0qnNh3bh1AjOU5rNoB8gmI9GiC/VeX2PKWAfGdm76ZnyUqX+D 8ORbsW/XKvZbshVERPN+K6Fp5hOVLgdO1tg8BGwAjV+tpkzOsyz5hFHSMBs/AoLHOwgaGqD0p nthHOBuXAsEaZjHDOwEPq4HUKjr+Pqp1VSeC5TEBxaFB9S87O4Md640Pc69hjxXMKCBQ4PPYC VB5MUiSIvCsgREMd2VIHbdCMkSfja7G41FiNvKvXQNx704A3kioBZ1X4CHr9/cuYOJng9llhe AA0N1ld1iMSauPP8WZtrprLvdVf6e6crRB/L6GNZK7SWMOXryBZnM9efHzWHDoh0QNb+/3h44 rhysG89FTs94IvfT5yWzJFTNOZkLjSEV32X3YsZ/0danXD9sycH+GLr9iCAQJyqqZ9aVnau1m 2Xyttfhx1jEV2S+j0aOIvZCf3XJR2g7HGPb0AZ+9KqsBkqbHWDsvmuA2ZH02rLfRVNYPzSyrN 1F7hfM+8Acr3o3BiwXBTVp6eteDgZ/VQfke6j5wxzaFe4E5XedBOb/oj3sDjVrFJ4qdqJBYNh ADfx11B1K9esBWq9dLqk8A1HNT5qflRgDRjlW4f4axd3uFjUfaURTkiQkweZnXPk+7m3HYU01 6xulgVWyzYCjtGmO3ft8zfXA== X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787744014820777254 X-GMAIL-MSGID: 1787744014820777254 Dear all, we are accepting loop annotations IVDEP, UNROLL n, VECTOR, and NOVECTOR for ordinary do loops, but ICE when such an annotation is specified before a DO CONCURRENT loop. Since at least the Intel compilers recognize some of the annotations also for DO CONCURRENT, it seems natural to extend gfortran instead of rejecting or ignoring the attributes. The attached patch handles the annotations as needed for the control structures of FORALL/DO CONCURRENT. Regarding the UNROLL directive, I don't have good references, so feedback is welcome. The current patch applies UNROLL only to the first loop control variable (for the case of loop nests), which translates into the innermost loop in gcc's representation. Regtested on x86_64-pc-linux-gnu. OK for mainline? Comments? Thanks, Harald From 0df60f02c399a6bf65850ecd5719b25b3de6676f Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Wed, 10 Jan 2024 23:10:02 +0100 Subject: [PATCH] Fortran: annotations for DO CONCURRENT loops [PR113305] gcc/fortran/ChangeLog: PR fortran/113305 * gfortran.h: Add annotation controls to gfc_forall_iterator. * gfortran.texi: Document annotations IVDEP, UNROLL n, VECTOR, NOVECTOR as applied to DO CONCURRENT. * parse.cc (parse_do_block): Parse annotations IVDEP, UNROLL n, VECTOR, NOVECTOR as applied to DO CONCURRENT. Apply UNROLL only to first loop control variable. * trans-stmt.cc (gfc_trans_forall_loop): Annotate loops with IVDEP, UNROLL n, VECTOR, NOVECTOR as needed for DO CONCURRENT. (gfc_trans_forall_1): Handle annotations IVDEP, UNROLL n, VECTOR, NOVECTOR. gcc/testsuite/ChangeLog: PR fortran/113305 * gfortran.dg/do_concurrent_7.f90: New test. --- gcc/fortran/gfortran.h | 4 +++ gcc/fortran/gfortran.texi | 12 ++++++++ gcc/fortran/parse.cc | 26 ++++++++++++++++- gcc/fortran/trans-stmt.cc | 29 ++++++++++++++++++- gcc/testsuite/gfortran.dg/do_concurrent_7.f90 | 26 +++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/do_concurrent_7.f90 diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 82f388c05f8..88502c1e3f0 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -2926,6 +2926,10 @@ gfc_dt; typedef struct gfc_forall_iterator { gfc_expr *var, *start, *end, *stride; + unsigned short unroll; + bool ivdep; + bool vector; + bool novector; struct gfc_forall_iterator *next; } gfc_forall_iterator; diff --git a/gcc/fortran/gfortran.texi b/gcc/fortran/gfortran.texi index 5615fee2897..371666dcbb6 100644 --- a/gcc/fortran/gfortran.texi +++ b/gcc/fortran/gfortran.texi @@ -3262,6 +3262,9 @@ It must be placed immediately before a @code{DO} loop and applies only to the loop that follows. N is an integer constant specifying the unrolling factor. The values of 0 and 1 block any unrolling of the loop. +For @code{DO CONCURRENT} constructs the unrolling specification applies +only to the first loop control variable. + @node BUILTIN directive @subsection BUILTIN directive @@ -3300,6 +3303,9 @@ whether a particular loop is vectorizable due to potential dependencies between iterations. The purpose of the directive is to tell the compiler that vectorization is safe. +For @code{DO CONCURRENT} constructs this annotation is implicit to all +loop control variables. + This directive is intended for annotation of existing code. For new code it is recommended to consider OpenMP SIMD directives as potential alternative. @@ -3316,6 +3322,9 @@ This directive tells the compiler to vectorize the following loop. It must be placed immediately before a @code{DO} loop and applies only to the loop that follows. +For @code{DO CONCURRENT} constructs this annotation applies to all loops +specified in the concurrent header. + @node NOVECTOR directive @subsection NOVECTOR directive @@ -3328,6 +3337,9 @@ This directive tells the compiler to not vectorize the following loop. It must be placed immediately before a @code{DO} loop and applies only to the loop that follows. +For @code{DO CONCURRENT} constructs this annotation applies to all loops +specified in the concurrent header. + @node Non-Fortran Main Program @section Non-Fortran Main Program diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc index d8b38cfb5ac..f41cc7d3510 100644 --- a/gcc/fortran/parse.cc +++ b/gcc/fortran/parse.cc @@ -5307,7 +5307,31 @@ parse_do_block (void) do_op = new_st.op; s.ext.end_do_label = new_st.label1; - if (new_st.ext.iterator != NULL) + if (do_op == EXEC_DO_CONCURRENT) + { + gfc_forall_iterator *fa; + for (fa = new_st.ext.forall_iterator; fa; fa = fa->next) + { + /* Apply unroll only to innermost loop (first control + variable). */ + if (directive_unroll != -1) + { + fa->unroll = directive_unroll; + directive_unroll = -1; + } + if (directive_ivdep) + fa->ivdep = directive_ivdep; + if (directive_vector) + fa->vector = directive_vector; + if (directive_novector) + fa->novector = directive_novector; + } + directive_ivdep = false; + directive_vector = false; + directive_novector = false; + stree = NULL; + } + else if (new_st.ext.iterator != NULL) { stree = new_st.ext.iterator->var->symtree; if (directive_unroll != -1) diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index a718dce237f..59a9cf99f9b 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -41,6 +41,10 @@ typedef struct iter_info tree start; tree end; tree step; + unsigned short unroll; + bool ivdep; + bool vector; + bool novector; struct iter_info *next; } iter_info; @@ -4117,12 +4121,30 @@ gfc_trans_forall_loop (forall_info *forall_tmp, tree body, /* PR 83064 means that we cannot use annot_expr_parallel_kind until the autoparallelizer can handle this. */ - if (forall_tmp->do_concurrent) + if (forall_tmp->do_concurrent || iter->ivdep) cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond, build_int_cst (integer_type_node, annot_expr_ivdep_kind), integer_zero_node); + if (iter->unroll && cond != error_mark_node) + cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond, + build_int_cst (integer_type_node, + annot_expr_unroll_kind), + build_int_cst (integer_type_node, iter->unroll)); + + if (iter->vector && cond != error_mark_node) + cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond, + build_int_cst (integer_type_node, + annot_expr_vector_kind), + integer_zero_node); + + if (iter->novector && cond != error_mark_node) + cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond, + build_int_cst (integer_type_node, + annot_expr_no_vector_kind), + integer_zero_node); + tmp = build1_v (GOTO_EXPR, exit_label); tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp, build_empty_stmt (input_location)); @@ -5090,6 +5112,11 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info) gfc_add_block_to_block (&block, &se.pre); step[n] = se.expr; + this_forall->unroll = fa->unroll; + this_forall->ivdep = fa->ivdep; + this_forall->vector = fa->vector; + this_forall->novector = fa->novector; + /* Set the NEXT field of this_forall to NULL. */ this_forall->next = NULL; /* Link this_forall to the info construct. */ diff --git a/gcc/testsuite/gfortran.dg/do_concurrent_7.f90 b/gcc/testsuite/gfortran.dg/do_concurrent_7.f90 new file mode 100644 index 00000000000..604f6712d05 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/do_concurrent_7.f90 @@ -0,0 +1,26 @@ +! { dg-do compile } +! { dg-additional-options "-fdump-tree-original" } +! PR fortran/113305 + +program dc + implicit none + real :: a(12), b(12), c(16,8), d(16,8) + integer :: i, j + call random_number(b) +!GCC$ ivdep +!GCC$ vector + do concurrent (i=1:12) + a(i) = 2*b(i) + end do + c = b(1) + d = a(2) +!GCC$ novector +!GCC$ unroll 4 + do concurrent (i=1:16:2,j=1:8:2) + d(i,j) = 3*c(i,j) + end do +end program + +! { dg-final { scan-tree-dump "ANNOTATE_EXPR .* ivdep>, vector" "original" } } +! { dg-final { scan-tree-dump "ANNOTATE_EXPR .* ivdep>, no-vector" "original" } } +! { dg-final { scan-tree-dump "ANNOTATE_EXPR .* ivdep>, unroll 4>, no-vector" "original" } } -- 2.35.3