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: 119184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1159975vqm; Wed, 12 Jul 2023 06:44:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlEhfgfG9rFWOL+S5FekE9z65xPP1n0YmxkwbnLX9OVjQhRhNweD4GdgMuiYY4E4VmtHoYrK X-Received: by 2002:aa7:dc05:0:b0:51f:e0f0:c634 with SMTP id b5-20020aa7dc05000000b0051fe0f0c634mr2070708edu.14.1689169441834; Wed, 12 Jul 2023 06:44:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689169441; cv=none; d=google.com; s=arc-20160816; b=zoxxZuiGNGlgk8+QqKdR/DPRlQ1iFrbsoROzIifLOVs/fZ+CocdS60pPk9KS6px6Qp tSc7WRft5JxtqfajhiQiq31wR8CgtY7mTOpxDkBOQHOPr5qfWKxzBS95eFftVt+xzzX8 TgsfdW7oZ0vjp4vt9fTiHamuUmsDdA3Go/d8qYtlE+8Gq7eqAvfuD9ogzZ5LB4fEKA8c iHbP4pVG9aanUFXHX/fQJ/4oYh78qhYpyQPkXabgtsYF8UaoM7u4PQugEEuzaRvUG2CF qZw1pdVtAkbQcBzyhTdbog4xTXjlbXpSLkvrjvgSczz7OonWcgIbkbbgceZkIMv8vlZi a9Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:ironport-sdr:dmarc-filter:delivered-to; bh=dceOnnUo1f71wxIHqj9oHAMA6jlOdD4kn3wKJVb3POE=; fh=Ct/e3b2va2oMa0rm9QhMMSw0+/YuRB79hnN1PxV+RU8=; b=f6GYNVbkFURuoBd+JYeP2/GkTnMkteREYUQ508lTRkEdcVQV9XEHbT2jYcA3fPvlpJ dZKxx9+HxrKh/TklYxPULC3ZTQz6LAehIs/ZqrvkljEJjd0EURaMHommfBs1C6kC4Dt3 BsaAbbWir76ZZ7BC1r5JJ9bKffLpfj0IKwPxVs7jo2FNYh3t0mDl0EOZ/YbBsm6E//bP 8xOqnVOxpyVZ+m3LcUbi02JYjoURWy8wL8gMWF2Wsq4Qz9fMezTeGBP7rkOA1uNG4cKi 5VQFsSxa+ANpQSnCd8r8o1Stn3XMrP1raPayhCDWiW1dn1bgC6DLz2S7rTvZ7WHoG0kM 981Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c24-20020a056402121800b0051e0252e3e9si4463875edw.362.2023.07.12.06.44.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 06:44:01 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7108F385AF8F for ; Wed, 12 Jul 2023 13:43:01 +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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771222536957376741 X-GMAIL-MSGID: 1771222536957376741 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);