From patchwork Fri May 24 20:02:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kwok Cheung Yeung X-Patchwork-Id: 1939085 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=dYSec0uj; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VmGFC4GvHz1ynR for ; Sat, 25 May 2024 06:03:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AFD2C3858C48 for ; Fri, 24 May 2024 20:03:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 89712385EC55 for ; Fri, 24 May 2024 20:03:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89712385EC55 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=baylibre.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 89712385EC55 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716580984; cv=none; b=FE8oKH0DW7c4pxZPKi0cqbwJJkBSFn+bfAjQIjBSWpZHBk8GsC/8bRCsQSBETN2OxSjJ0bd4KefmFkE+r1rqjKnsQb873gfMQQbmEO+/xR1cg716d8Xk5eLcP1q7crfTp5ObImFGtMSsjdrQumCxbEB7P11uhleGuMtd0qT2vN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716580984; c=relaxed/simple; bh=FRbjAPR9KtdavOcysArkwzHtYsgggkV1rnzLgDMSOc8=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:Subject:From; b=InbYJw52eapY3qmrzIijKwq2gHbWXD84rMqrYrGnag/wB6izcIuhCB5e3DBFTOlrBC+q0vGgGxvcshNBIcfG12ne7qyw0JfkSquOFG11aRWmrGho+AZmfqSbYrVpk6MiK7UjuLdHjYSF3istrVRPG1lh6GXmUHR5Hc+S8AgHzpE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-354de97586cso4140237f8f.0 for ; Fri, 24 May 2024 13:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1716580979; x=1717185779; darn=gcc.gnu.org; h=in-reply-to:from:content-language:subject:references:to:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=pOm+axmQRo3Su1QdjoZSGduZcvIeKG0CTxD7dU+ZtPc=; b=dYSec0uj9fFsnL5aUSa5BnZJrX9XZmEgL/hpbZlZywD+8FL70z4VJ7tedcypPk7e0q XVIg362uwomiJXWGWV8qKVyH7pnCpB4UrHjsqZ3bZ44cW7d+vQ+xX5h2m3gKdOERZ7JC 8N64S4zm0ByvCJf6t0juXYOfGYCicFrPfj4maK0g5/EwmbujVhu7MufLfKpMbOfvuMv1 LOjFr3RiaWLpVMJtZmjW4MSxE2egff/j3buUGJIF4K50nJvrHZvQwKRTfZ6ljqg5PoVo ZDhZODoyHoRAMlGDta1KMH1gzpfU4Ror/cre1O+437Ovj9hXWX9gIotLZu0K1mQUj7sh eg1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716580979; x=1717185779; h=in-reply-to:from:content-language:subject:references:to:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=pOm+axmQRo3Su1QdjoZSGduZcvIeKG0CTxD7dU+ZtPc=; b=VdEii7DI11hnRNbveE5L7ZbNIaAjjmq7JHL4aSMXHn9EwoeFirk7A/RAZp8ud/x5qS VznGEHL81lOvLgYf0ZWhM+VbdIg/wyv6BKLEkbTwdmKAM4MoZ5S0rhNecOcjSYBRUbNw 9hOtOH3ldbOz/py+VtWab4UES2Ss1ocwi2R4nEEBjwhqtPEXw8KhfTDa37CCDcza0aqD fFUB5tQCP8H7r9BTHUapoQUd+G2gXi2st/a5FhpQ4ZYTD6TD2aSZAMEq420o17XI7CkY Ero06wSJmnqFs660MSXtUGlrgl/onCpvnEyCEg53udERW1ldy9Yc8yazOgFrXKBZBtEP SRiw== X-Forwarded-Encrypted: i=1; AJvYcCX1bCNcxw/vWUV6q7CopKPBpwERrRIym1Oygo+brmYtQFqFS5ewwy2ypqPfhreiwKUxlux6xSe3tJkSOdpWsqO0YQcUz7ttKA== X-Gm-Message-State: AOJu0YyDmRrvBHrW4WLcjM46fCvczHfhWLTM3pP5QlDJuVPW0+HdzmMA qGATuv7jXKoUXqzbV65eqOEuuOM5JPyhYAwOrZrhgKdPiJvK4Zy3fz96LHWh7IbQ/0S4RBNzB+Z V X-Google-Smtp-Source: AGHT+IHlqN4MbmeRa0Y/a95NX5/Ox/6F/a1kmQEYh41Uo2yZYUPIpRolrGTlMbm28MU+YvRKmmjBhA== X-Received: by 2002:a5d:504d:0:b0:354:f168:9862 with SMTP id ffacd0b85a97d-355221273fbmr2494847f8f.0.1716580979065; Fri, 24 May 2024 13:02:59 -0700 (PDT) Received: from ?IPV6:2a00:23c6:88e9:8c01:a4cf:c86c:d8f4:e5b8? ([2a00:23c6:88e9:8c01:a4cf:c86c:d8f4:e5b8]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557a1c9363sm2288446f8f.72.2024.05.24.13.02.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 May 2024 13:02:58 -0700 (PDT) Message-ID: <274d63cc-9cf3-4d98-9863-383c7045fcac@baylibre.com> Date: Fri, 24 May 2024 21:02:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Jakub Jelinek , Tobias Burnus , gcc-patches References: Subject: [PATCH 3/3] openmp: Add support for iterators in to/from clauses (C/C++) Content-Language: en-GB From: Kwok Cheung Yeung In-Reply-To: X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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+incoming=patchwork.ozlabs.org@gcc.gnu.org This patch extends the previous patch to cover to/from clauses in 'target update'. From 99addc124535307b50fbdeb66c4f90bb0cbeb041 Mon Sep 17 00:00:00 2001 From: Kwok Cheung Yeung Date: Mon, 15 Apr 2024 13:50:22 +0100 Subject: [PATCH 3/3] openmp: Add support for iterators in to/from clauses (C/C++) This adds support for iterators in 'to' and 'from' clauses in the 'target update' OpenMP directive. 2024-05-24 Kwok Cheung Yeung gcc/c/ * c-parser.cc (c_parser_omp_clause_from_to): Parse 'iterator' modifier. gcc/cp/ * parser.cc (cp_parser_omp_clause_from_to): Parse 'iterator' modifier. gcc/ * gimplify.cc (gimplify_omp_map_iterators): Gimplify iterators in to/from clauses. (gimplify_scan_omp_clauses): Call gimplify_omp_map_iterators once to handle clauses with iterators, then skip subsequent iterator clauses. * omp-low.cc (scan_sharing_clauses): Skip firstprivate handling for to/from clauses with iterators. (lower_omp_target): Handle kinds for to/from clauses with iterators. * tree-pretty-print.cc (dump_omp_clause): Call dump_omp_map_iterators for to/from clauses with iterators. gcc/testsuite/ * c-c++-common/gomp/target-update-iterator-1.c: New. * c-c++-common/gomp/target-update-iterator-2.c: New. * c-c++-common/gomp/target-update-iterator-3.c: New. libgomp/ * target.c (gomp_update): Call gomp_merge_iterator_maps. Free allocated variables. * testsuite/libgomp.c-c++-common/target-update-iterators-1.c: New. * testsuite/libgomp.c-c++-common/target-update-iterators-2.c: New. * testsuite/libgomp.c-c++-common/target-update-iterators-3.c: New. --- gcc/c/c-parser.cc | 105 ++++++++++++++-- gcc/cp/parser.cc | 116 ++++++++++++++++-- gcc/gimplify.cc | 17 ++- gcc/omp-low.cc | 24 +++- .../gomp/target-update-iterator-1.c | 20 +++ .../gomp/target-update-iterator-2.c | 17 +++ .../gomp/target-update-iterator-3.c | 17 +++ gcc/tree-pretty-print.cc | 20 ++- libgomp/target.c | 12 ++ .../target-update-iterators-1.c | 65 ++++++++++ .../target-update-iterators-2.c | 57 +++++++++ .../target-update-iterators-3.c | 66 ++++++++++ 12 files changed, 509 insertions(+), 27 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/gomp/target-update-iterator-1.c create mode 100644 gcc/testsuite/c-c++-common/gomp/target-update-iterator-2.c create mode 100644 gcc/testsuite/c-c++-common/gomp/target-update-iterator-3.c create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-1.c create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-2.c create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-3.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 2281148561c..6353b15d64f 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -19185,8 +19185,11 @@ c_parser_omp_clause_device_type (c_parser *parser, tree list) to ( variable-list ) OpenMP 5.1: - from ( [present :] variable-list ) - to ( [present :] variable-list ) */ + from ( [motion-modifier[,] [motion-modifier[,]...]:] variable-list ) + to ( [motion-modifier[,] [motion-modifier[,]...]:] variable-list ) + + motion-modifier: + present | iterator (iterators-definition) */ static tree c_parser_omp_clause_from_to (c_parser *parser, enum omp_clause_code kind, @@ -19197,15 +19200,88 @@ c_parser_omp_clause_from_to (c_parser *parser, enum omp_clause_code kind, if (!parens.require_open (parser)) return list; + int pos = 1, colon_pos = 0; + int iterator_length = 0; + while (c_parser_peek_nth_token_raw (parser, pos)->type == CPP_NAME) + { + if (c_parser_peek_nth_token_raw (parser, pos + 1)->type + == CPP_OPEN_PAREN) + { + unsigned int n = pos + 2; + if (c_parser_check_balanced_raw_token_sequence (parser, &n) + && (c_parser_peek_nth_token_raw (parser, n)->type + == CPP_CLOSE_PAREN)) + { + iterator_length = n - pos + 1; + pos = n; + } + } + if (c_parser_peek_nth_token_raw (parser, pos + 1)->type == CPP_COMMA) + pos += 2; + else + pos++; + if (c_parser_peek_nth_token_raw (parser, pos)->type == CPP_COLON) + { + colon_pos = pos; + break; + } + } + bool present = false; - c_token *token = c_parser_peek_token (parser); + tree iterators = NULL_TREE; - if (token->type == CPP_NAME - && strcmp (IDENTIFIER_POINTER (token->value), "present") == 0 - && c_parser_peek_2nd_token (parser)->type == CPP_COLON) + for (pos = 1; pos < colon_pos; pos++) { - present = true; - c_parser_consume_token (parser); + c_token *token = c_parser_peek_token (parser); + + if (token->type == CPP_COMMA) + { + c_parser_consume_token (parser); + continue; + } + if (token->type == CPP_NAME) + { + const char *name = IDENTIFIER_POINTER (token->value); + if (strcmp (name, "present") == 0) + { + if (present) + { + c_parser_error (parser, "too many % modifiers"); + parens.skip_until_found_close (parser); + return list; + } + present = true; + c_parser_consume_token (parser); + } + else if (strcmp (name, "iterator") == 0) + { + if (iterators) + { + c_parser_error (parser, "too many % modifiers"); + parens.skip_until_found_close (parser); + return list; + } + iterators = c_parser_omp_iterators (parser); + pos += iterator_length - 1; + } + else + { + if (kind == OMP_CLAUSE_TO) + c_parser_error (parser, "% clause with motion modifier " + "other than % or %"); + else + c_parser_error (parser, "% clause with motion modifier " + "other than % or %"); + parens.skip_until_found_close (parser); + return list; + } + } + } + + if (colon_pos) + { + gcc_assert (pos == colon_pos); + gcc_assert (c_parser_next_token_is (parser, CPP_COLON)); c_parser_consume_token (parser); } @@ -19216,6 +19292,19 @@ c_parser_omp_clause_from_to (c_parser *parser, enum omp_clause_code kind, for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) OMP_CLAUSE_MOTION_PRESENT (c) = 1; + if (iterators) + { + tree block = pop_scope (); + if (iterators == error_mark_node) + iterators = NULL_TREE; + else + TREE_VEC_ELT (iterators, 5) = block; + } + + if (iterators) + for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) + OMP_CLAUSE_DECL (c) = build_tree_list (iterators, OMP_CLAUSE_DECL (c)); + return nl; } diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 6dc67851f96..f26da16ca13 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -41612,8 +41612,11 @@ cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc) to ( variable-list ) OpenMP 5.1: - from ( [present :] variable-list ) - to ( [present :] variable-list ) */ + from ( [motion-modifier[,] [motion-modifier[,]...]:] variable-list ) + to ( [motion-modifier[,] [motion-modifier[,]...]:] variable-list ) + + motion-modifier: + present | iterator (iterators-definition) */ static tree cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind, @@ -41622,15 +41625,94 @@ cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind, if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) return list; + size_t pos = 1, colon_pos = 0; + int iterator_length = 0; + while (cp_lexer_nth_token_is (parser->lexer, pos, CPP_NAME)) + { + if (cp_lexer_nth_token_is (parser->lexer, pos + 1, CPP_OPEN_PAREN)) + { + unsigned int n = cp_parser_skip_balanced_tokens (parser, pos + 1); + if (n != pos + 1) + { + iterator_length = n - pos; + pos = n - 1; + } + } + if (cp_lexer_nth_token_is (parser->lexer, pos + 1, CPP_COMMA)) + pos += 2; + else + pos++; + if (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COLON)) + { + colon_pos = pos; + break; + } + } + bool present = false; - cp_token *token = cp_lexer_peek_token (parser->lexer); + tree iterators = NULL_TREE; + for (pos = 1; pos < colon_pos; pos++) + { + cp_token *token = cp_lexer_peek_token (parser->lexer); - if (token->type == CPP_NAME - && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0 - && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)) + if (token->type == CPP_COMMA) + { + cp_lexer_consume_token (parser->lexer); + continue; + } + if (token->type == CPP_NAME) + { + const char *name = IDENTIFIER_POINTER (token->u.value); + if (strcmp (name, "present") == 0) + { + if (present) + { + cp_parser_error (parser, "too many % modifiers"); + cp_parser_skip_to_closing_parenthesis (parser, + /*recovering=*/true, + /*or_comma=*/false, + /*consume_paren=*/true); + return list; + } + present = true; + cp_lexer_consume_token (parser->lexer); + } + else if (strcmp (name, "iterator") == 0) + { + if (iterators) + { + cp_parser_error (parser, "too many % modifiers"); + cp_parser_skip_to_closing_parenthesis (parser, + /*recovering=*/true, + /*or_comma=*/false, + /*consume_paren=*/true); + return list; + } + begin_scope (sk_omp, NULL); + iterators = cp_parser_omp_iterators (parser); + pos += iterator_length - 1; + } + else + { + if (kind == OMP_CLAUSE_TO) + cp_parser_error (parser, "% clause with motion modifier " + "other than % or %"); + else + cp_parser_error (parser, "% clause with motion modifier " + "other than % or %"); + cp_parser_skip_to_closing_parenthesis (parser, + /*recovering=*/true, + /*or_comma=*/false, + /*consume_paren=*/true); + return list; + } + } + } + + if (colon_pos) { - present = true; - cp_lexer_consume_token (parser->lexer); + gcc_assert (pos == colon_pos); + gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_COLON)); cp_lexer_consume_token (parser->lexer); } @@ -41639,6 +41721,19 @@ cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind, for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) OMP_CLAUSE_MOTION_PRESENT (c) = 1; + if (iterators) + { + tree block = poplevel (1, 1, 0); + if (iterators == error_mark_node) + iterators = NULL_TREE; + else + TREE_VEC_ELT (iterators, 5) = block; + } + + if (iterators) + for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) + OMP_CLAUSE_DECL (c) = build_tree_list (iterators, OMP_CLAUSE_DECL (c)); + return nl; } @@ -41684,9 +41779,8 @@ cp_parser_omp_clause_map (cp_parser *parser, tree list) && strcmp (IDENTIFIER_POINTER (tok->u.value), "iterator") == 0 && next_tok->type == CPP_OPEN_PAREN) { - size_t n = cp_parser_skip_balanced_tokens (parser, 2); - if (cp_lexer_peek_nth_token (parser->lexer, n - 1)->type - == CPP_CLOSE_PAREN) + int n = cp_parser_skip_balanced_tokens (parser, pos + 1); + if (n != pos + 1) { iterator_length = n - pos; pos = n - 1; diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index cadb2a3d96d..3cf88cf82b5 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -9304,7 +9304,9 @@ gimplify_omp_map_iterators (tree *list_p, gimple_seq *pre_p) while (tree c = *list_p) { - if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) + if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP + && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TO + && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FROM) { list_p = &OMP_CLAUSE_CHAIN (c); continue; @@ -12937,6 +12939,19 @@ gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p, case OMP_CLAUSE_FROM: case OMP_CLAUSE__CACHE_: decl = OMP_CLAUSE_DECL (c); + + if (OMP_ITERATOR_DECL_P (decl)) + { + if (!handled_map_iterators) + { + gimplify_omp_map_iterators (list_p, pre_p); + handled_map_iterators = true; + continue; + } + /* Skip declarations with iterators. */ + break; + } + if (error_operand_p (decl)) { remove = true; diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index d0f0c5d884a..9e94ad329cd 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -1519,8 +1519,10 @@ scan_sharing_clauses (tree clauses, omp_context *ctx) if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c))) { /* FIXME: Is this the right way to handle these? */ - if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER - || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE) + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP + && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER + || OMP_CLAUSE_MAP_KIND (c) + == GOMP_MAP_FIRSTPRIVATE_REFERENCE)) break; tree field = build_decl (OMP_CLAUSE_LOCATION (c), @@ -13061,7 +13063,23 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx) purpose = size_int (map_idx++); CONSTRUCTOR_APPEND_ELT (vsize, purpose, size_int (SIZE_MAX)); - unsigned HOST_WIDE_INT tkind = OMP_CLAUSE_MAP_KIND (c); + unsigned HOST_WIDE_INT tkind = 0; + switch (OMP_CLAUSE_CODE (c)) + { + case OMP_CLAUSE_TO: + tkind = (OMP_CLAUSE_MOTION_PRESENT (c) + ? GOMP_MAP_ALWAYS_PRESENT_TO : GOMP_MAP_TO); + break; + case OMP_CLAUSE_FROM: + tkind = (OMP_CLAUSE_MOTION_PRESENT (c) + ? GOMP_MAP_ALWAYS_PRESENT_FROM : GOMP_MAP_FROM); + break; + case OMP_CLAUSE_MAP: + tkind = OMP_CLAUSE_MAP_KIND (c); + break; + default: + gcc_unreachable (); + } gcc_checking_assert (tkind < (HOST_WIDE_INT_C (1U) << talign_shift)); gcc_checking_assert (tkind diff --git a/gcc/testsuite/c-c++-common/gomp/target-update-iterator-1.c b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-1.c new file mode 100644 index 00000000000..3a64f511da4 --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-1.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-fopenmp" } */ + +#define DIM1 17 +#define DIM2 39 + +void f (int **x, float **y) +{ + #pragma omp target update to (iterator(i=0:DIM1): x[i][:DIM2]) + + #pragma omp target update to (iterator(i=0:DIM1): x[i][:DIM2], y[i][:DIM2]) + + #pragma omp target update to (iterator(i=0:DIM1), present: x[i][:DIM2]) + + #pragma omp target update to (iterator(i=0:DIM1), iterator(j=0:DIM2): x[i][j]) /* { dg-error "too many 'iterator' modifiers" } */ + /* { dg-error ".#pragma omp target update. must contain at least one .from. or .to. clauses" "" { target *-*-* } .-1 } */ + + #pragma omp target update to (iterator(i=0:DIM1), something: x[i][j]) /* { dg-error ".to. clause with motion modifier other than .iterator. or .present. before .something." } */ + /* { dg-error ".#pragma omp target update. must contain at least one .from. or .to. clauses" "" { target *-*-* } .-1 } */ +} diff --git a/gcc/testsuite/c-c++-common/gomp/target-update-iterator-2.c b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-2.c new file mode 100644 index 00000000000..3789a559b6f --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-2.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-fopenmp" } */ + +void f (int *x, float *y, double *z) +{ + #pragma omp target update to(iterator(i=0:10): x) /* { dg-error "iterator variable .i. not used in clause expression" }*/ + ; + + #pragma omp target update from(iterator(i=0:10, j=0:20): x[i]) /* { dg-error "iterator variable .j. not used in clause expression" }*/ + ; + + #pragma omp target update to(iterator(i=0:10, j=0:20, k=0:30): x[i], y[j], z[k]) + /* { dg-error "iterator variable .i. not used in clause expression" "" { target *-*-* } .-1 } */ + /* { dg-error "iterator variable .j. not used in clause expression" "" { target *-*-* } .-2 } */ + /* { dg-error "iterator variable .k. not used in clause expression" "" { target *-*-* } .-3 } */ + ; +} diff --git a/gcc/testsuite/c-c++-common/gomp/target-update-iterator-3.c b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-3.c new file mode 100644 index 00000000000..d8672b3a242 --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/target-update-iterator-3.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-fopenmp -fdump-tree-gimple" } */ + +#define DIM1 10 +#define DIM2 20 +#define DIM3 30 + +void f (int ***x, float ***y, double **z) +{ + #pragma omp target update to (iterator(i=0:DIM1, j=0:DIM2): x[i][j][:DIM3], y[i][j][:DIM3]) + #pragma omp target update from (iterator(i=0:DIM1): z[i][:DIM2]) +} + +/* { dg-final { scan-tree-dump-times "if \\(i <= 9\\) goto ; else goto ;" 2 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "if \\(j <= 19\\) goto ; else goto ;" 1 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "to\\(iterator\\(int i=0:10:1, int j=0:20:1\\):iterator_array=D\.\[0-9\]+" 2 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "from\\(iterator\\(int i=0:10:1\\):iterator_array=D\.\[0-9\]+" 1 "gimple" } } */ diff --git a/gcc/tree-pretty-print.cc b/gcc/tree-pretty-print.cc index feb9c93a809..a0fe270ab1b 100644 --- a/gcc/tree-pretty-print.cc +++ b/gcc/tree-pretty-print.cc @@ -1099,16 +1099,28 @@ dump_omp_clause (pretty_printer *pp, tree clause, int spc, dump_flags_t flags) pp_string (pp, "from("); if (OMP_CLAUSE_MOTION_PRESENT (clause)) pp_string (pp, "present:"); - dump_generic_node (pp, OMP_CLAUSE_DECL (clause), - spc, flags, false); + decl = OMP_CLAUSE_DECL (clause); + if (OMP_ITERATOR_DECL_P (decl)) + { + dump_omp_map_iterators (pp, TREE_PURPOSE (decl), spc, flags); + pp_colon (pp); + decl = TREE_VALUE (decl); + } + dump_generic_node (pp, decl, spc, flags, false); goto print_clause_size; case OMP_CLAUSE_TO: pp_string (pp, "to("); if (OMP_CLAUSE_MOTION_PRESENT (clause)) pp_string (pp, "present:"); - dump_generic_node (pp, OMP_CLAUSE_DECL (clause), - spc, flags, false); + decl = OMP_CLAUSE_DECL (clause); + if (OMP_ITERATOR_DECL_P (decl)) + { + dump_omp_map_iterators (pp, TREE_PURPOSE (decl), spc, flags); + pp_colon (pp); + decl = TREE_VALUE (decl); + } + dump_generic_node (pp, decl, spc, flags, false); goto print_clause_size; case OMP_CLAUSE__CACHE_: diff --git a/libgomp/target.c b/libgomp/target.c index 07ba840b495..71a6428af49 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -2221,6 +2221,7 @@ gomp_update (struct gomp_device_descr *devicep, size_t mapnum, void **hostaddrs, size_t i; struct splay_tree_key_s cur_node; const int typemask = short_mapkind ? 0xff : 0x7; + bool iterators_p = false; if (!devicep) return; @@ -2228,6 +2229,10 @@ gomp_update (struct gomp_device_descr *devicep, size_t mapnum, void **hostaddrs, if (mapnum == 0) return; + if (short_mapkind) + iterators_p = gomp_merge_iterator_maps (&mapnum, &hostaddrs, &sizes, + &kinds); + gomp_mutex_lock (&devicep->lock); if (devicep->state == GOMP_DEVICE_FINALIZED) { @@ -2321,6 +2326,13 @@ gomp_update (struct gomp_device_descr *devicep, size_t mapnum, void **hostaddrs, } } gomp_mutex_unlock (&devicep->lock); + + if (iterators_p) + { + free (hostaddrs); + free (sizes); + free (kinds); + } } static struct gomp_offload_icv_list * diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-1.c b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-1.c new file mode 100644 index 00000000000..5a4cad5c219 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-1.c @@ -0,0 +1,65 @@ +/* { dg-do run } */ + +/* Test target enter data and target update to the target using map + iterators. */ + +#include + +#define DIM1 8 +#define DIM2 15 + +int mkarray (int *x[]) +{ + int expected = 0; + for (int i = 0; i < DIM1; i++) + { + x[i] = (int *) malloc (DIM2 * sizeof (int)); + for (int j = 0; j < DIM2; j++) + { + x[i][j] = rand (); + expected += x[i][j]; + } + } + + return expected; +} + +int main (void) +{ + int *x[DIM1]; + int sum; + int expected = mkarray (x); + + #pragma omp target enter data map(to: x[:DIM1]) + #pragma omp target enter data map(iterator(i=0:DIM1), to: x[i][:DIM2]) + #pragma omp target map(from: sum) + { + sum = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + sum += x[i][j]; + } + + if (sum != expected) + return 1; + + expected = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + { + x[i][j] *= rand (); + expected += x[i][j]; + } + + #pragma omp target update to(iterator(i=0:DIM1): x[i][:DIM2]) + + #pragma omp target map(from: sum) + { + sum = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + sum += x[i][j]; + } + + return sum != expected; +} diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-2.c b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-2.c new file mode 100644 index 00000000000..949cc266d84 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-2.c @@ -0,0 +1,57 @@ +/* { dg-do run } */ + +/* Test target enter data and target update from the target using map + iterators. */ + +#include + +#define DIM1 8 +#define DIM2 15 + +void mkarray (int *x[]) +{ + for (int i = 0; i < DIM1; i++) + { + x[i] = (int *) malloc (DIM2 * sizeof (int)); + for (int j = 0; j < DIM2; j++) + x[i][j] = 0; + } +} + +int main (void) +{ + int *x[DIM1]; + int sum, expected; + + mkarray (x); + + #pragma omp target enter data map(alloc: x[:DIM1]) + #pragma omp target enter data map(iterator(i=0:DIM1), to: x[i][:DIM2]) + #pragma omp target map(from: expected) + { + expected = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + { + x[i][j] = (i + 1) * (j + 2); + expected += x[i][j]; + } + } + + /* Host copy of x should remain unchanged. */ + sum = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + sum += x[i][j]; + if (sum != 0) + return 1; + + #pragma omp target update from(iterator(i=0:DIM1): x[i][:DIM2]) + + /* Host copy should now be updated. */ + sum = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + sum += x[i][j]; + return sum - expected; +} diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-3.c b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-3.c new file mode 100644 index 00000000000..852635e50f6 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/target-update-iterators-3.c @@ -0,0 +1,66 @@ +/* { dg-do run } */ + +/* Test target enter data and target update to the target using map + iterators with a function. */ + +#include + +#define DIM1 8 +#define DIM2 15 + +void mkarray (int *x[]) +{ + for (int i = 0; i < DIM1; i++) + { + x[i] = (int *) malloc (DIM2 * sizeof (int)); + for (int j = 0; j < DIM2; j++) + x[i][j] = rand (); + } +} + +int f (int i) +{ + return i * 2; +} + +int main (void) +{ + int *x[DIM1], x_new[DIM1][DIM2]; + int sum, expected; + + mkarray (x); + + #pragma omp target enter data map(alloc: x[:DIM1]) + #pragma omp target enter data map(iterator(i=0:DIM1), to: x[i][:DIM2]) + + /* Update x on host. */ + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + { + x_new[i][j] = x[i][j]; + x[i][j] = (i + 1) * (j + 2); + } + + /* Update a subset of x on target. */ + #pragma omp target update to(iterator(i=0:DIM1/2): x[f (i)][:DIM2]) + + #pragma omp target map(from: sum) + { + sum = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + sum += x[i][j]; + } + + /* Calculate expected value on host. */ + for (int i = 0; i < DIM1/2; i++) + for (int j = 0; j < DIM2; j++) + x_new[f (i)][j] = x[f (i)][j]; + + expected = 0; + for (int i = 0; i < DIM1; i++) + for (int j = 0; j < DIM2; j++) + expected += x_new[i][j]; + + return sum - expected; +}