From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1534) id 9F91C3858D3C; Tue, 12 Sep 2023 09:24:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F91C3858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694510683; bh=Dvb3tvto2hgV46+6gWmd8n0gNLC9wFaabOPK5YuihsU=; h=From:To:Subject:Date:From; b=uEVU9hjHmg7AlcrCOGA8xZeAX7DDtBpXTdlVfS7KIe9G4x3uL3DY/erA2lnaVhQr/ /MY7CgbeFvpnpbUAH68EVM+CiDwOjJ+64snxrle5QHIwKEfbaKh6uWZFtriYFtNYuO FugmHUR5DWBaXBap/7B9NGsjbLIki6v3KciC6OLQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Tobias Burnus To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-13] OpenMP (C only): For 'omp allocate', really walk tree for 'alloctor' check X-Act-Checkin: gcc X-Git-Author: Tobias Burnus X-Git-Refname: refs/heads/devel/omp/gcc-13 X-Git-Oldrev: e03fb2bd2339bd953261fa619bfec1967404b26c X-Git-Newrev: 26302558b9b91d70e723cd028863e357c7af109b Message-Id: <20230912092443.9F91C3858D3C@sourceware.org> Date: Tue, 12 Sep 2023 09:24:43 +0000 (GMT) List-Id: https://gcc.gnu.org/g:26302558b9b91d70e723cd028863e357c7af109b commit 26302558b9b91d70e723cd028863e357c7af109b Author: Tobias Burnus Date: Tue Sep 12 11:24:22 2023 +0200 OpenMP (C only): For 'omp allocate', really walk tree for 'alloctor' check Walk expression tree of the 'allocator' clause of 'omp allocate' to detect more cases where the allocator expression depends on code between a variable declaration and its associated '#pragma omp allocate'. It also contains the fix for the 'allocator((omp_allocator_handle_t)-1)' ICE, also tested for in previous commit. The changes of this commit were supposed to be part of r14-3863-g35f498d8dfc8e579eaba2ff2d2b96769c632fd58 OpenMP (C only): omp allocate - extend parsing support, improve diagnostic which also contains the associated testcase changes but were left out (oops!). gcc/c/ChangeLog: * c-parser.cc (struct c_omp_loc_tree): New. (c_check_omp_allocate_allocator_r): New; checking moved from ... (c_parser_omp_allocate): ... here. Call it via walk_tree. Avoid ICE with tree_to_shwi for invalid too-large value. (cherry picked from commit 27144cc05c4e12f58998b6e30d23098664dd51db) Diff: --- gcc/c/ChangeLog.omp | 10 ++++++ gcc/c/c-parser.cc | 102 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/gcc/c/ChangeLog.omp b/gcc/c/ChangeLog.omp index 46ff6e4d539e..31ca350a3af0 100644 --- a/gcc/c/ChangeLog.omp +++ b/gcc/c/ChangeLog.omp @@ -1,3 +1,13 @@ +2023-09-12 Tobias Burnus + + Backported from master: + 2023-09-12 Tobias Burnus + + * c-parser.cc (struct c_omp_loc_tree): New. + (c_check_omp_allocate_allocator_r): New; checking moved from ... + (c_parser_omp_allocate): ... here. Call it via walk_tree. Avoid + ICE with tree_to_shwi for invalid too-large value. + 2023-09-12 Tobias Burnus Backported from master: diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index ba6cbbbcf1db..734eb495ea5e 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -19977,6 +19977,61 @@ c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name) return stmt; } +struct c_omp_loc_tree +{ + location_t loc; + tree var; +}; + +/* Check whether the expression used in the allocator clause is declared or + modified between the variable declaration and its allocate directive. */ +static tree +c_check_omp_allocate_allocator_r (tree *tp, int *, void *data) +{ + tree var = ((struct c_omp_loc_tree *) data)->var; + location_t loc = ((struct c_omp_loc_tree *) data)->loc; + if (TREE_CODE (*tp) == VAR_DECL && c_check_in_current_scope (*tp)) + { + if (linemap_location_before_p (line_table, DECL_SOURCE_LOCATION (var), + DECL_SOURCE_LOCATION (*tp))) + { + error_at (loc, "variable %qD used in the % clause must " + "be declared before %qD", *tp, var); + inform (DECL_SOURCE_LOCATION (*tp), "declared here"); + inform (DECL_SOURCE_LOCATION (var), + "to be allocated variable declared here"); + return *tp; + } + else + { + gcc_assert (cur_stmt_list + && TREE_CODE (cur_stmt_list) == STATEMENT_LIST); + + tree_stmt_iterator l = tsi_last (cur_stmt_list); + while (!tsi_end_p (l)) + { + if (linemap_location_before_p (line_table, EXPR_LOCATION (*l), + DECL_SOURCE_LOCATION (var))) + break; + if (TREE_CODE (*l) == MODIFY_EXPR + && TREE_OPERAND (*l, 0) == *tp) + { + error_at (loc, + "variable %qD used in the % clause " + "must not be modified between declaration of %qD " + "and its % directive", *tp, var); + inform (EXPR_LOCATION (*l), "modified here"); + inform (DECL_SOURCE_LOCATION (var), + "to be allocated variable declared here"); + return *tp; + } + --l; + } + } + } + return NULL_TREE; +} + /* OpenMP 5.x: # pragma omp allocate (list) clauses @@ -20099,8 +20154,8 @@ c_parser_omp_allocate (c_parser *parser) error_at (loc, "% clause required for " "static variable %qD", var); else if (allocator - && (tree_int_cst_sgn (allocator) != 1 - || tree_to_shwi (allocator) > 8)) + && (wi::to_widest (allocator) < 1 + || wi::to_widest (allocator) > 8)) /* 8 = largest predefined memory allocator. */ error_at (allocator_loc, "% clause requires a predefined allocator as " @@ -20111,46 +20166,11 @@ c_parser_omp_allocate (c_parser *parser) "%qD not yet supported", var); continue; } - if (allocator - && TREE_CODE (allocator) == VAR_DECL - && c_check_in_current_scope (var)) + if (allocator) { - if (linemap_location_before_p (line_table, DECL_SOURCE_LOCATION (var), - DECL_SOURCE_LOCATION (allocator))) - { - error_at (OMP_CLAUSE_LOCATION (nl), - "allocator variable %qD must be declared before %qD", - allocator, var); - inform (DECL_SOURCE_LOCATION (allocator), - "allocator declared here"); - inform (DECL_SOURCE_LOCATION (var), "declared here"); - } - else - { - gcc_assert (cur_stmt_list - && TREE_CODE (cur_stmt_list) == STATEMENT_LIST); - tree_stmt_iterator l = tsi_last (cur_stmt_list); - while (!tsi_end_p (l)) - { - if (linemap_location_before_p (line_table, EXPR_LOCATION (*l), - DECL_SOURCE_LOCATION (var))) - break; - if (TREE_CODE (*l) == MODIFY_EXPR - && TREE_OPERAND (*l, 0) == allocator) - { - error_at (EXPR_LOCATION (*l), - "allocator variable %qD, used in the " - "% directive for %qD, must not be " - "modified between declaration of %qD and its " - "% directive", - allocator, var, var); - inform (DECL_SOURCE_LOCATION (var), "declared here"); - inform (OMP_CLAUSE_LOCATION (nl), "used here"); - break; - } - --l; - } - } + struct c_omp_loc_tree data + = {EXPR_LOC_OR_LOC (allocator, OMP_CLAUSE_LOCATION (nl)), var}; + walk_tree (&allocator, c_check_omp_allocate_allocator_r, &data, NULL); } DECL_ATTRIBUTES (var) = tree_cons (get_identifier ("omp allocate"), build_tree_list (allocator, alignment),