From patchwork Tue Nov 23 09:54:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 48024 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5624B3858033 for ; Tue, 23 Nov 2021 09:55:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5624B3858033 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637661327; bh=wqm2dVx4ljytHklPMt8mjPKKbBaLKJczCFipQj70NiM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=bQaR6LQ6X8e+4cpKhGtvKmRcGK7JGr2UQ8WZa9EeJJcP2NRBL0IP9cWCsKWxIkPqq 0G5wc8/tUBMPeR5QFbiJ7T6rYA8wtH5Hnq1JiYkkNVOaMCKLBwQaj/qoTVISpIt1Ni gByHbpsCHknTeB5FMJ5ONKjlZIwag0FtjUEyHmMk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E93713858D28 for ; Tue, 23 Nov 2021 09:54:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E93713858D28 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-347-Z83mn6DiMZOUthVWS50J2w-1; Tue, 23 Nov 2021 04:54:52 -0500 X-MC-Unique: Z83mn6DiMZOUthVWS50J2w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 15B4787D541; Tue, 23 Nov 2021 09:54:51 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8BE4B794AB; Tue, 23 Nov 2021 09:54:50 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1AN9slmM3952349 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 23 Nov 2021 10:54:48 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1AN9sk1O3952348; Tue, 23 Nov 2021 10:54:46 +0100 Date: Tue, 23 Nov 2021 10:54:46 +0100 To: gcc-patches@gcc.gnu.org Subject: [committed] openmp: Fix up handling of reduction clauses on the loop construct [PR102431] Message-ID: <20211123095446.GS2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: Tobias Burnus Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! We were using unshare_expr and walk_tree_without_duplicate replacement of the placeholder vars. The OMP_CLAUSE_REDUCTION_{INIT,MERGE} can contain other trees that need to be duplicated though, e.g. BLOCKs referenced in BIND_EXPR(s), or local VAR_DECLs. This patch uses the inliner code to copy all of that. There is a slight complication that those local VAR_DECLs or placeholders don't have DECL_CONTEXT set, they will get that only when they are gimplified later on, so this patch sets DECL_CONTEXT for those temporarily and resets it afterwards. Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2021-11-23 Jakub Jelinek PR middle-end/102431 * gimplify.c (replace_reduction_placeholders): Remove. (note_no_context_vars): New function. (gimplify_omp_loop): For OMP_PARALLEL's BIND_EXPR create a new BLOCK. Use copy_tree_body_r with walk_tree instead of unshare_expr and replace_reduction_placeholders for duplication of OMP_CLAUSE_REDUCTION_{INIT,MERGE} expressions. Ensure all mentioned automatic vars have DECL_CONTEXT set to non-NULL before doing so and reset it afterwards for those vars and their corresponding vars. * c-c++-common/gomp/pr102431.c: New test. * g++.dg/gomp/pr102431.C: New test. * gfortran.dg/gomp/pr102431.f90: New test. Jakub --- gcc/gimplify.c.jj 2021-11-17 17:28:51.000000000 +0100 +++ gcc/gimplify.c 2021-11-22 16:14:18.365451780 +0100 @@ -13128,21 +13128,15 @@ gimplify_omp_for (tree *expr_p, gimple_s /* Helper for gimplify_omp_loop, called through walk_tree. */ static tree -replace_reduction_placeholders (tree *tp, int *walk_subtrees, void *data) +note_no_context_vars (tree *tp, int *, void *data) { - if (DECL_P (*tp)) + if (VAR_P (*tp) + && DECL_CONTEXT (*tp) == NULL_TREE + && !is_global_var (*tp)) { - tree *d = (tree *) data; - if (*tp == OMP_CLAUSE_REDUCTION_PLACEHOLDER (d[0])) - { - *tp = OMP_CLAUSE_REDUCTION_PLACEHOLDER (d[1]); - *walk_subtrees = 0; - } - else if (*tp == OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (d[0])) - { - *tp = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (d[1]); - *walk_subtrees = 0; - } + vec *d = (vec *) data; + d->safe_push (*tp); + DECL_CONTEXT (*tp) = current_function_decl; } return NULL_TREE; } @@ -13312,7 +13306,8 @@ gimplify_omp_loop (tree *expr_p, gimple_ { if (pass == 2) { - tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); + tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, + make_node (BLOCK)); append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind)); *expr_p = make_node (OMP_PARALLEL); TREE_TYPE (*expr_p) = void_type_node; @@ -13379,25 +13374,64 @@ gimplify_omp_loop (tree *expr_p, gimple_ *pc = copy_node (c); OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c)); TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c)); - OMP_CLAUSE_REDUCTION_INIT (*pc) - = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c)); - OMP_CLAUSE_REDUCTION_MERGE (*pc) - = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c)); if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)) { + auto_vec no_context_vars; + int walk_subtrees = 0; + note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c), + &walk_subtrees, &no_context_vars); + if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)) + note_no_context_vars (&p, &walk_subtrees, &no_context_vars); + walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c), + note_no_context_vars, + &no_context_vars); + walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c), + note_no_context_vars, + &no_context_vars); + OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc) = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)); if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)) OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc) = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)); - tree nc = *pc; - tree data[2] = { c, nc }; - walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (nc), - replace_reduction_placeholders, - data); - walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (nc), - replace_reduction_placeholders, - data); + + hash_map decl_map; + decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c)); + decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c), + OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)); + if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)) + decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c), + OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)); + + copy_body_data id; + memset (&id, 0, sizeof (id)); + id.src_fn = current_function_decl; + id.dst_fn = current_function_decl; + id.src_cfun = cfun; + id.decl_map = &decl_map; + id.copy_decl = copy_decl_no_change; + id.transform_call_graph_edges = CB_CGE_DUPLICATE; + id.transform_new_cfg = true; + id.transform_return_to_modify = false; + id.transform_lang_insert_block = NULL; + id.eh_lp_nr = 0; + walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r, + &id, NULL); + walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r, + &id, NULL); + + for (tree d : no_context_vars) + { + DECL_CONTEXT (d) = NULL_TREE; + DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE; + } + } + else + { + OMP_CLAUSE_REDUCTION_INIT (*pc) + = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c)); + OMP_CLAUSE_REDUCTION_MERGE (*pc) + = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c)); } pc = &OMP_CLAUSE_CHAIN (*pc); break; --- gcc/testsuite/c-c++-common/gomp/pr102431.c.jj 2021-11-22 16:32:54.375567302 +0100 +++ gcc/testsuite/c-c++-common/gomp/pr102431.c 2021-11-22 16:32:08.765217306 +0100 @@ -0,0 +1,16 @@ +/* PR middle-end/102431 */ + +struct S { int s; } s; +void add (struct S *, struct S *); +void init (struct S *); +void bar (int i, struct S *); +#pragma omp declare reduction (+:struct S:add (&omp_out, &omp_in)) initializer (init (&omp_priv)) + +void +foo (void) +{ + int i; + #pragma omp loop bind(teams) reduction(+:s) + for (i = 0; i < 8; i++) + bar (i, &s); +} --- gcc/testsuite/g++.dg/gomp/pr102431.C.jj 2021-11-22 16:33:41.057902021 +0100 +++ gcc/testsuite/g++.dg/gomp/pr102431.C 2021-11-22 16:33:25.728120489 +0100 @@ -0,0 +1,13 @@ +// PR middle-end/102431 + +struct S { S (); ~S (); S (const S &); void add (const S &); int s; } s; +void bar (int, S &); +#pragma omp declare reduction (+:S:omp_out.add (omp_in)) + +void +foo () +{ + #pragma omp loop bind(teams) reduction(+:s) + for (int i = 0; i < 8; i++) + bar (i, s); +} --- gcc/testsuite/gfortran.dg/gomp/pr102431.f90.jj 2021-11-22 16:41:53.312886774 +0100 +++ gcc/testsuite/gfortran.dg/gomp/pr102431.f90 2021-11-22 16:44:17.914826016 +0100 @@ -0,0 +1,10 @@ +! PR middle-end/102431 + +program pr102431 + integer :: a(2) + a(:) = 0 + !$omp parallel loop reduction(+:a) + do i = 1, 8 + a = a + 1 + end do +end