From patchwork Fri Aug 18 17:15:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 136045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp1555250vqi; Fri, 18 Aug 2023 10:16:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEorSntFfwhjnJNj3usbw4nGnFh3qbOBb0ONuYqRz5x+XrScAY7MtwD6BolTOJ3h5ThGOTf X-Received: by 2002:a17:906:3da9:b0:969:7739:2eb7 with SMTP id y9-20020a1709063da900b0096977392eb7mr2041599ejh.4.1692378960970; Fri, 18 Aug 2023 10:16:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692378960; cv=none; d=google.com; s=arc-20160816; b=khX7WZnP0dNJ95ZXx9ANWhv6MCgazIjLwfnuhCjsI7QSc8EbMS5itdVJJgqtag2HhX +h3SSPMKhNoOoWZJSdz1EzlfBCcboTK515Gx5HVY/ZvUB4EXAskWBOwD5dbsQrOSxYWO 7kMrs3SR/SmH5y0o2Lo5CfaxFwLB5cDylsZHGL4xu0m/6G29xsPqCbVELrYOxTLkCitG LG/tR2s5tbUhG6j261YDN5DdE2w1k0xnDDpX33m4yf6bwudO5FgHiCRf3rFST79d/QP5 9648FdW0HRgCdykS/wQe13JDoDE6RwHj3meIC/CMoX2/zsDoMbIq4xIU7hCkzgtndnxA K4Jg== 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:subject:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=DknY4xRmarRcX2BnwKmQlrq6K6W+xvo93oLbhADK4B8=; fh=VgTg9Kw/OoQCQxLP3fgo/h2QKSxiDasIhTZoWFyzVac=; b=vZLsv+7lq9uPUyJxiR+kcljXhaHX8cNBfYPMQ5ZDyGp1GM1g2WRL2TddUvqO86An6Q JS0p6EZt2FMH+wPmVeXwBX0RuE8Nsmb9dq623Bng8/8azObsrR3S3vUT4DJjspn3Kt+k JZOQhW+rj2cBtphgb7xMjyb19q14Z4v4/j8To7dAL5E67TyCCG791VpQQx67MTb4Ev+a QzlJt6heRv8RYX4MF7BvEVYBSBUBGF+G3zkCavqEyOd/88cDLRp8r/I+/uSLfwRXLmJ6 sUKH0IALW8laXGlyiT/hdZoydUpVraQOuNSwo+B360Zi0kkluF2R65P8Tqh8Pn7OGp4a Vpsg== 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 bt26-20020a170906b15a00b0099bd6b14081si1508202ejb.927.2023.08.18.10.16.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Aug 2023 10:16:00 -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 8C070387689E for ; Fri, 18 Aug 2023 17:15:54 +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 31E3B3860765 for ; Fri, 18 Aug 2023 17:15:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31E3B3860765 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,183,1684828800"; d="diff'?scan'208";a="16738944" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 18 Aug 2023 09:15:21 -0800 IronPort-SDR: oOnG3RnmCJPtMojnbVQyoGvniCvUKHh9ge6CsmEGKGGkmp6SM3BBZfKeE4PxeMlqFYoHTfoRg7 FO2vjxk2MlKApFxN40ZiR2zYiJjFSK7f5LOIQUlUxGdMJzYYRK3Ey01ximlaAIFhcjtZvKSVzp b8N7BvrCNODV23sqAWCHdY0HeinX4zO2x0oxZyvPvsHeTdwYtR5rCf+aapQ/6mFFs64aT1c5Nf BZTV41r95C9De8BPUvzyFdRHYy5cSPnPJ8IQ3aHIa7mLzri+aGG4QHlDSYZVOHwIb6XgxYWZLu MCQ= Message-ID: <07c94dde-f513-0177-51d7-05267694f383@codesourcery.com> Date: Fri, 18 Aug 2023 19:15:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Content-Language: en-US To: gcc-patches , Jakub Jelinek From: Tobias Burnus Subject: [Patch] omp-expand.cc: Fix wrong code with non-rectangular loop nest [PR111017] X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-15.mgc.mentorg.com (139.181.222.15) To svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, 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.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: 1774587961321375431 X-GMAIL-MSGID: 1774587961321375431 This patch fixes a bug with an OpenMP non-rectangular loop nest where the factor is 0. With the old code before r12-5295-g47de0b56ee455e, the testcase of the PR (or included in the attached patch) worked fine. omp-expand.c contained back then: https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/omp-expand.c;hb=eacdfaf7ca07367ede1a0c50aa997953958dabae#l2560 2560 gcond *cond_stmt 2561 = gimple_build_cond (NE_EXPR, factor, 2562 build_zero_cst (TREE_TYPE (factor)), 2563 NULL_TREE, NULL_TREE); 2564 gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING); In commit https://gcc.gnu.org/r12-5295-g47de0b56ee455e a new function was introduced: +/* Prepend or append LHS CODE RHS condition before or after *GSI_P. */ + +static gcond * +expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code, + tree lhs, tree rhs, bool after = false) +{ + gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE); + if (after) + gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING); + else + gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT); While it supports both before/GSI_SAME_STMT and after/GSI_CONTINUE_LINKING, the patch missed to add an '/* after= */ true for the 'factor != 0' condition above. (For all others, after=false was fine.) This patch reinstates the prior after/GSI_CONTINUE_LINKING by adding 'true' to the call and, thus, fixes the in between segfaulting testcase of the PR, https://gcc.gnu.org/PR111017 Comments, questions, concerns? If not, I intent to commit the attached patch to mainline on Monday and after the usual grace time to GCC 13 and then to GCC 12. Tobias ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 omp-expand.cc: Fix wrong code with non-rectangular loop nest [PR111017] Before commit r12-5295-g47de0b56ee455e, all gimple_build_cond in expand_omp_for_* were inserted with gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT); except the one dealing with the multiplicative factor that was gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING); That commit for PR103208 fixed the issue of some missing regimplify of operands of GIMPLE_CONDs by moving the condition handling to the new function expand_omp_build_cond. While that function has an 'bool after = false' argument to switch between the two variants. However, all callers ommited this argument. This commit reinstates the prior behavior by passing 'true' for the factor != 0 condition, fixing the included testcase. PR middle-end/111017 gcc/ * omp-expand.cc (expand_omp_for_init_vars): Pass after=true to expand_omp_build_cond for 'factor != 0' condition, resulting in pre-r12-5295-g47de0b56ee455e code for the gimple insert. libgomp/ * testsuite/libgomp.c-c++-common/non-rect-loop-1.c: New test. --- gcc/omp-expand.cc | 3 +- .../libgomp.c-c++-common/non-rect-loop-1.c | 72 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/gcc/omp-expand.cc b/gcc/omp-expand.cc index db58b3cb49b..1a4d625fea3 100644 --- a/gcc/omp-expand.cc +++ b/gcc/omp-expand.cc @@ -2562,7 +2562,8 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi, tree factor = fd->factor; gcond *cond_stmt = expand_omp_build_cond (gsi, NE_EXPR, factor, - build_zero_cst (TREE_TYPE (factor))); + build_zero_cst (TREE_TYPE (factor)), + true); edge e = split_block (gsi_bb (*gsi), cond_stmt); basic_block bb0 = e->src; e->flags = EDGE_TRUE_VALUE; diff --git a/libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c b/libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c new file mode 100644 index 00000000000..fbd462b3683 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c @@ -0,0 +1,72 @@ +/* PR middle-end/111017 */ + +#include + +#define DIM 32 +#define N (DIM*DIM) + +int +main () +{ + int a[N], b[N], c[N]; + int dim = DIM; + + for (int i = 0; i < N; i++) + { + a[i] = 3*i; + b[i] = 7*i; + c[i] = 42; + } + + #pragma omp parallel for collapse(2) + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + c[j] = a[j] + b[j]; + + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j) + __builtin_abort (); + for (int i = 0; i < N; i++) + c[i] = 42; + + #pragma omp parallel for collapse(2) + for (int i = 0; i < dim; i++) + for (int j = (i*dim); j < (i*dim + dim); j++) + c[j] = a[j] + b[j]; + + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j) + __builtin_abort (); + for (int i = 0; i < N; i++) + c[i] = 42; + + for (int dev = 0; dev <= omp_get_num_devices(); dev++) + { + #pragma omp target teams loop device(dev) map(to:a,b) map(from:c) + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + c[j] = a[j] + b[j]; + + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j) + __builtin_abort (); + for (int i = 0; i < N; i++) + c[i] = 42; + + #pragma omp target teams loop device(dev) map(to:a,b) map(from:c) + for (int i = 0; i < dim; i++) + for (int j = (i*dim); j < (i*dim + dim); j++) + c[j] = a[j] + b[j]; + + for (int i = 0; i < DIM; i++) + for (int j = (i*DIM); j < (i*DIM + DIM); j++) + if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j) + __builtin_abort (); + for (int i = 0; i < N; i++) + c[i] = 42; + } + return 0; +}