From patchwork Wed Mar 11 13:28:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 1252915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48ct8S576cz9sPk for ; Thu, 12 Mar 2020 00:28:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA2073940CF2; Wed, 11 Mar 2020 13:28:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa1.mentor.iphmx.com (esa1.mentor.iphmx.com [68.232.129.153]) by sourceware.org (Postfix) with ESMTPS id 0AC773940CF2 for ; Wed, 11 Mar 2020 13:28:50 +0000 (GMT) IronPort-SDR: 9eJocyUYU0dzxgsAsBaRFykY8nFz7myRzJgkB9Ne90fj4RKZ3Kic3DYA8POVvMHQNFEl0eHDwT ktdZQMoYV7I162UL4Z80s0Q9TSPkDm8fK+Fzh1ZD/rvkNmlovnptHetumhIEfYJE0EEW4jnzx7 S+iZxrPZ0n3/VxcHXU7eiyzQVht6WrkptbxQ8PSR+dTWr8vIarOeqSTM/KTPBkUphk5FFfRukm w8y1nqu7vu4LMfi08eiUccB7Q7+5CavoL2BrLZWHXJhc+pmxt5QLZ9ETb4VGfUNLXqR6Bi7r5u DBo= X-URL-LookUp-ScanningError: 1 X-IronPort-AV: E=Sophos;i="5.70,541,1574150400"; d="diff'?scan'208";a="48572133" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 11 Mar 2020 05:28:49 -0800 IronPort-SDR: f9kuUZH2Ztj93U1+5Z4Thub0w6qJ45DoxiAFErYpKsMibDIwmqdGh7rXFJXiENOOcG3QOGhF8r GekaoEGon/NmHmcQkLSKXdD7KmjYPqi3AffcIZGv0M0RklkNDeAcWEAS90xOBwAj8Q4/8YvRfZ eboFX8CJG4YSY/V4VHsi1jG4wkZxMwi1jEhMG2kIuyIs0ocIvTXxhCEr0pnDS/ugzEqLHhR/gE PrDnyGNMxjjSdWIh+lzOYOKcCW8x4jV6eqSiMyvhx5uo+97DwVumQTCxCTqj0a9zmf7/3/DaSa Bcw= To: gcc-patches , Thomas Schwinge From: Tobias Burnus Subject: [C/C++, OpenACC] Reject vars of different scope in acc declare (PR94120) Message-ID: <0e98c5c9-7536-e11f-c42e-4e8060b147a5@codesourcery.com> Date: Wed, 11 Mar 2020 14:28:44 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 Content-Language: en-US X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-02.mgc.mentorg.com (139.181.222.2) To SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) X-Spam-Status: No, score=-26.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE, SPF_PASS 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: 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Fortran patch: https://gcc.gnu.org/pipermail/gcc-patches/current/541774.html Like for Fortran, it also fixes some other issues – here for C++ related to namespaces. (For class, see PR c++/94140.) Test case of the PR yields an ICE in the middle end and the namespace tests an ICE in cc1plus. Additionally, invalid code is not diagnosed. The OpenACC spec has under "Declare Directive" has the following restriction: "A declare directive must be in the same scope as the declaration of any var that appears in the data clauses of the directive." ("A declare directive is used […] following a variable declaration in C or C++".) NOTE for C++: This patch assumes that variables in a namespace are handled in the same way as those which are at global (namespace) scope; however, the OpenACC specification's wording currently is "In C or C++ global scope, only …". Hence, one can argue about this part of the patch; but as it fixes an ICE and is a very sensible extension – the other option is to reject it – I believe it is fine. (On the OpenACC side, this is now Issue 288.) OK for the trunk? Tobias ----------------- Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander Walter [C/C++, OpenACC] Reject vars of different scope in acc declare (PR94120) 2020-10-11 Tobias Burnus gcc/c/ PR middle-end/94120 * c-decl.c (c_check_oacc_same_scope): New function. * c-tree.h (c_check_oacc_same_scope): Declare it. * c-parser.c (c_parser_oacc_declare): Add check that variables are declared in the same scope as the directive. Fix handling of namespace vars. gcc/c/ PR middle-end/94120 * paser.c (cp_parser_oacc_declare): Add check that variables are declared in the same scope as the directive. gcc/testsuite/ PR middle-end/94120 * c-c++-common/goacc/declare-pr94120.c: New. * g++.dg/declare-pr94120.C: New. libgomp/testsuite/ PR middle-end/94120 * libgomp.oacc-c++/declare-pr94120.C: New. gcc/c/c-decl.c | 8 +++ gcc/c/c-parser.c | 9 ++++ gcc/c/c-tree.h | 1 + gcc/cp/parser.c | 21 +++++++- gcc/testsuite/c-c++-common/goacc/declare-pr94120.c | 23 +++++++++ gcc/testsuite/g++.dg/declare-pr94120.C | 30 ++++++++++++ .../testsuite/libgomp.oacc-c++/declare-pr94120.C | 57 ++++++++++++++++++++++ 7 files changed, 147 insertions(+), 2 deletions(-) diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index c819fd0d0d5..eda95d664de 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -12016,4 +12016,12 @@ c_check_omp_declare_reduction_r (tree *tp, int *, void *data) return NULL_TREE; } + +bool +c_check_oacc_same_scope (tree decl) +{ + struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl)); + return b != NULL && B_IN_CURRENT_SCOPE (b); +} + #include "gt-c-c-decl.h" diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 1e8f2f7108d..63e8ab0ad17 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -16573,6 +16573,15 @@ c_parser_oacc_declare (c_parser *parser) break; } + if (!c_check_oacc_same_scope (decl)) + { + error_at (loc, + "%qD must be a variable declared in the same scope as " + "%<#pragma acc declare%>", decl); + error = true; + continue; + } + if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)) || lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (decl))) diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 71229927cb6..6d578705d77 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -789,6 +789,7 @@ extern tree c_omp_reduction_id (enum tree_code, tree); extern tree c_omp_reduction_decl (tree); extern tree c_omp_reduction_lookup (tree, tree); extern tree c_check_omp_declare_reduction_r (tree *, int *, void *); +extern bool c_check_oacc_same_scope (tree); extern void c_pushtag (location_t, tree, tree); extern void c_bind (location_t, tree, bool); extern bool tag_exists_p (enum tree_code, tree); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 24f71671469..8f09eb0d375 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -40722,6 +40722,7 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok) { tree clauses, stmt; bool error = false; + bool found_in_scope = global_bindings_p (); clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK, "#pragma acc declare", pragma_tok, true); @@ -40794,6 +40795,22 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok) break; } + if (!found_in_scope) + for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d)) + if (d == decl) + { + found_in_scope = true; + break; + } + if (!found_in_scope) + { + error_at (loc, + "%qD must be a variable declared in the same scope as " + "%<#pragma acc declare%>", decl); + error = true; + continue; + } + if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)) || lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (decl))) @@ -40815,7 +40832,7 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok) DECL_ATTRIBUTES (decl) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl)); - if (global_bindings_p ()) + if (current_binding_level->kind == sk_namespace) { symtab_node *node = symtab_node::get (decl); if (node != NULL) @@ -40832,7 +40849,7 @@ cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok) } } - if (error || global_bindings_p ()) + if (error || current_binding_level->kind == sk_namespace) return NULL_TREE; stmt = make_node (OACC_DECLARE); diff --git a/gcc/testsuite/c-c++-common/goacc/declare-pr94120.c b/gcc/testsuite/c-c++-common/goacc/declare-pr94120.c new file mode 100644 index 00000000000..21b2cc14fc7 --- /dev/null +++ b/gcc/testsuite/c-c++-common/goacc/declare-pr94120.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ + +/* PR middle-end/94120 */ + +void foo() +{ + int foo; + { + #pragma acc declare copy(foo) /* { dg-error "'foo' must be a variable declared in the same scope as '#pragma acc declare'" } */ + } +} + +void +f_data (void) +{ + int B[10]; +#pragma acc data + { +# pragma acc declare copy(B) /* { dg-error "'B' must be a variable declared in the same scope as '#pragma acc declare'" } */ + for (int i = 0; i < 10; i++) + B[i] = -i; + } +} diff --git a/gcc/testsuite/g++.dg/declare-pr94120.C b/gcc/testsuite/g++.dg/declare-pr94120.C new file mode 100644 index 00000000000..8515c4ff875 --- /dev/null +++ b/gcc/testsuite/g++.dg/declare-pr94120.C @@ -0,0 +1,30 @@ +/* { dg-do compile } */ + +/* PR middle-end/94120 */ + +int b[8]; +#pragma acc declare create (b) + +namespace my { + int d[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + #pragma acc declare copyin (d) +}; + +namespace outer { + namespace inner { + int e[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + #pragma acc declare copyin (e) + }; +}; + +int f[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; +namespace my { + #pragma acc declare copyin (f) +}; + +namespace outer { + int g[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + namespace inner { + #pragma acc declare copyin (g) + }; +}; diff --git a/libgomp/testsuite/libgomp.oacc-c++/declare-pr94120.C b/libgomp/testsuite/libgomp.oacc-c++/declare-pr94120.C new file mode 100644 index 00000000000..1e1254187ea --- /dev/null +++ b/libgomp/testsuite/libgomp.oacc-c++/declare-pr94120.C @@ -0,0 +1,57 @@ +#include +#include + +#define N 8 + +namespace one { + int A[N] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + #pragma acc declare copyin (A) +}; + +namespace outer { + namespace inner { + int B[N]; + #pragma acc declare create (B) + }; +}; + +static void +f (void) +{ + int i; + int C[N]; + #pragma acc declare copyout (C) + + if (!acc_is_present (&one::A, sizeof (one::A))) + abort (); + + if (!acc_is_present (&outer::inner::B, sizeof (outer::inner::B))) + abort (); + +#pragma acc parallel + for (i = 0; i < N; i++) + { + outer::inner::B[i] = one::A[i]; + C[i] = outer::inner::B[i]; + } + + for (i = 0; i < N; i++) + { + if (C[i] != i + 1) + abort (); + } + +#pragma acc parallel + for (i = 0; i < N; i++) + if (outer::inner::B[i] != i + 1) + abort (); +} + + +int +main (int argc, char **argv) +{ + f (); + + return 0; +}