From patchwork Wed Jul 12 13:42:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Brown X-Patchwork-Id: 1806819 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1JpM1kCsz20cD for ; Wed, 12 Jul 2023 23:42:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E1CA3854831 for ; Wed, 12 Jul 2023 13:42:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id E0A4E3858C1F; Wed, 12 Jul 2023 13:42:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E0A4E3858C1F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="6.01,199,1684828800"; d="scan'208";a="12995786" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 12 Jul 2023 05:42:26 -0800 IronPort-SDR: ezWEqBybk3UwqAAzPjGL9ipVhZxLglSf5Vz+aaRtshIFWgbkNBRkWIEd2axK85nOvKbNqUpjzc t6XM8JFDqV8y+ze3OFa8kQNL8p60Ow15Sr1/9r/W1urjchkeh1AiPnUBfNp/6xklCuARviJYQg t3eXGR7jsWyH75s6PetssU+HlpSpnCpBdnZpzEgs+KiTHzFIJU70MZWsRJN/NVd/2Vb3sqUC+r ZgCsxTlyaJOhW0HEaljt1Eq4OzGGSnVXYLWnzLwK/vwMvAGvE50Frh7wNlfxHsjwku/5Wv8mvg jfI= From: Julian Brown To: CC: , , Subject: [PATCH] OpenMP: Strided/rectangular 'target update' out-of-bounds array lookup fix Date: Wed, 12 Jul 2023 06:42:07 -0700 Message-ID: <20230712134207.123424-2-julian@codesourcery.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230712134207.123424-1-julian@codesourcery.com> References: <20230712134207.123424-1-julian@codesourcery.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-11.mgc.mentorg.com (139.181.222.11) To svr-ies-mbx-11.mgc.mentorg.com (139.181.222.11) X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, 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.29 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 Sender: "Gcc-patches" This patch fixes a bug with the calculation of array bounds in the metadata for noncontiguous 'target update' directives. We record the array base address, a bias and the array length to pass to libgomp -- but at present, we use the 'whole array size' for the last, which means that at runtime we might look up an array with lower bound "base+bias" and upper bound "base+bias+length", which for non-zero bias will overflow the actual bounds of the array on the host and will (sometimes) return an unrelated block instead of the correct one. The fix is to instead calculate a size for the array that encloses the elements to be transferred, and is guaranteed to be entirely within the array (user errors excepted). Tested with offloading to NVPTX. I will apply (to og13) shortly. 2023-07-11 Julian Brown gcc/ * omp-low.cc (lower_omp_target): Calculate volume enclosing transferred elements instead of using whole array size for noncontiguous 'target update' operations. --- gcc/omp-low.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index 05ac917fb27..c7706a5921f 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -14444,11 +14444,13 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx) tree bias = size_zero_node; tree volume = size_one_node; + tree enclosure = size_one_node; for (i = dims - 1; i >= 0; i--) { tree dim = (*vdim)[i].value; tree index = (*vindex)[i].value; tree stride = (*vstride)[i].value; + tree len = (*vlen)[i].value; /* For the bias we want, e.g.: @@ -14463,6 +14465,20 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx) size_binop (MULT_EXPR, volume, index_stride)); volume = size_binop (MULT_EXPR, volume, dim); + + if (i == 0) + { + tree elems_covered = size_binop (MINUS_EXPR, len, + size_one_node); + elems_covered = size_binop (MULT_EXPR, elems_covered, + stride); + elems_covered = size_binop (PLUS_EXPR, elems_covered, + size_one_node); + enclosure = size_binop (MULT_EXPR, enclosure, + elems_covered); + } + else + enclosure = volume; } /* If we don't have a separate span size, use the element size @@ -14470,10 +14486,9 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx) if (!span) span = fold_convert (sizetype, elsize); - /* The size of the whole array -- to make sure we find any - part of the array via splay-tree lookup that might be - mapped on the target at runtime. */ - OMP_CLAUSE_SIZE (oc) = size_binop (MULT_EXPR, arrsize, span); + /* The size of a volume enclosing the elements to be + transferred. */ + OMP_CLAUSE_SIZE (oc) = size_binop (MULT_EXPR, enclosure, span); /* And the bias of the first element we will update. */ OMP_CLAUSE_SIZE (dn) = size_binop (MULT_EXPR, bias, span);