From patchwork Fri Jul 29 08:03:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp677509pxt; Fri, 29 Jul 2022 01:04:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tfXFv7mnV1ToWavaS+BUptYz705E1K3pYZ+PDRT+Y+LgXAn5ignJ4Xm4ANlMErP/QgmaRZ X-Received: by 2002:a17:907:3e88:b0:72f:9d01:a458 with SMTP id hs8-20020a1709073e8800b0072f9d01a458mr2013173ejc.107.1659081880724; Fri, 29 Jul 2022 01:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659081880; cv=none; d=google.com; s=arc-20160816; b=gRlOE+i7ow5MqCJOuOsCHqFrFvXvHpPLPm0fRk63I/g84yMcPl/9idJricWUgvlbUn Skuq5MDTbDr3lgmhPgFLEkuZ4n3taS9OHeZ/Bw+CcyaGpmH0rTX7YqsO8eR09A6pOY7F kvslVKxlnp44Z+Jhmuqh/H26MJvhQEmnWaEIdNLnc9w9g3PI0XwAKhjom5OLw6Fb3cVR f+ZFl0q0VnkNSHc9EYHf/6NAlcdWvh8au8aLQWnQmO2mtc2RN/L/syGPoyHaikFCIsLx 9zxfRjtpt4JRCKtmZWtrB2DiKZYg7FXRMIboPBwGdphy3TMFRDE6kskOcq4s2AY1hY07 qvHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=xG8U3SzNadR5n1vjiUEif1Wz82eIG+W5rvB8EPOEqLE=; b=iCAogYI2RG0uLNpQDjFRX7AYOV8UZ92zIQ4bewAYxS43t3G4TpAT8JnTcXBSg85VcV rdxZDCExd/u6eIJIWu768UNKgRkFN6wCE+Mp7ww8fHhN3GuPJnCE4m+EdO5/DtAxnAf5 4Fbfr2eKD0KvazLHzq/i3VUbhOnDlAoUFmDns6pDnS32L7nJHbFF1JFZjUIyngCIFRRx 6rj34rkHl1aagim/LKsYbMaug3Yf2fF1t3+B7dA/nVMVpWjalABHgALaNs3orNbVCynb XSGXzthKmqTZ529xMZjxjYvCAWbwyCB/TsWX3zkIbqUL0qjAFCs6tm4kMkzOeOs0WOgO kGzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mYLgKcQb; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bs1-20020a056402304100b0043ceeb58848si2582630edb.578.2022.07.29.01.04.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 01:04:40 -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; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mYLgKcQb; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A44F43857C6C for ; Fri, 29 Jul 2022 08:04:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A44F43857C6C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1659081879; bh=xG8U3SzNadR5n1vjiUEif1Wz82eIG+W5rvB8EPOEqLE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=mYLgKcQbL40s4tmykBoGHbPBh7LUIka42oo1ihHqhYsFNgVwbuxCijhSIZmUhKVtn 3pivajseOqm12Y+tEy4t1WX++hJdfvzG30V8VgrSjUVBNTRBa5v03UX/N1mohxn8PW oDlqFIv28qnuRu/AUjjixyw+5H2S6K4eXLTyCNYo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 23CFD3857B95 for ; Fri, 29 Jul 2022 08:03:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 23CFD3857B95 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-5-ANmXQr6ZMKGFB8aI7ijQ6w-1; Fri, 29 Jul 2022 04:03:52 -0400 X-MC-Unique: ANmXQr6ZMKGFB8aI7ijQ6w-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 433CE185A7BA; Fri, 29 Jul 2022 08:03:52 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0314A18EB7; Fri, 29 Jul 2022 08:03:51 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 26T83meW234413 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 29 Jul 2022 10:03:49 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 26T83lht234411; Fri, 29 Jul 2022 10:03:47 +0200 Date: Fri, 29 Jul 2022 10:03:47 +0200 To: gcc-patches@gcc.gnu.org Subject: [committed] openmp: Fix up handling of non-rectangular simd loops with pointer type iterators [PR106449] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: Tobias Burnus Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739673442429468578?= X-GMAIL-MSGID: =?utf-8?q?1739673442429468578?= Hi! There were 2 issues visible on this new testcase, one that we didn't have special POINTER_TYPE_P handling in a few spots of expand_omp_simd - for pointers we need to use POINTER_PLUS_EXPR and need to have the non-pointer part in sizetype, for non-rectangular loop on the other side we can rely on multiplication factor 1, pointers can't be multiplied, without those changes we'd ICE. The other issue was that we put n2 expression directly into a comparison in a condition and regimplified that, for the &a[512] case that and with gimplification being destructed that unfortunately meant modification of original fd->loops[?].n2. Fixed by unsharing the expression. This was causing a runtime failure on the testcase. Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2022-07-29 Jakub Jelinek PR middle-end/106449 * omp-expand.cc (expand_omp_simd): Fix up handling of pointer iterators in non-rectangular simd loops. Unshare fd->loops[i].n2 or n2 before regimplifying it inside of a condition. * testsuite/libgomp.c-c++-common/pr106449.c: New test. Jakub --- gcc/omp-expand.cc.jj 2022-06-28 13:03:30.930689700 +0200 +++ gcc/omp-expand.cc 2022-07-28 10:47:03.138939297 +0200 @@ -6714,7 +6696,7 @@ expand_omp_simd (struct omp_region *regi if (fd->loops[i].m2) t = n2v = create_tmp_var (itype); else - t = fold_convert (itype, fd->loops[i].n2); + t = fold_convert (itype, unshare_expr (fd->loops[i].n2)); t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, false, GSI_CONTINUE_LINKING); tree v = fd->loops[i].v; @@ -6728,7 +6710,7 @@ expand_omp_simd (struct omp_region *regi if (fd->collapse > 1 && !broken_loop) t = n2var; else - t = fold_convert (type, n2); + t = fold_convert (type, unshare_expr (n2)); t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, false, GSI_CONTINUE_LINKING); tree v = fd->loop.v; @@ -6840,7 +6819,7 @@ expand_omp_simd (struct omp_region *regi if (fd->loops[i].m2) t = nextn2v = create_tmp_var (itype); else - t = fold_convert (itype, fd->loops[i].n2); + t = fold_convert (itype, unshare_expr (fd->loops[i].n2)); t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, false, GSI_CONTINUE_LINKING); tree v = fd->loops[i].v; @@ -6870,17 +6849,25 @@ expand_omp_simd (struct omp_region *regi ne->probability = e->probability.invert (); gsi = gsi_after_labels (init_bb); - t = fold_convert (TREE_TYPE (fd->loops[i + 1].v), - fd->loops[i + 1].n1); if (fd->loops[i + 1].m1) { - tree t2 = fold_convert (TREE_TYPE (t), + tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v), fd->loops[i + 1 - fd->loops[i + 1].outer].v); - tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1); - t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3); - t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2); + if (POINTER_TYPE_P (TREE_TYPE (t2))) + t = fold_build_pointer_plus (t2, fd->loops[i + 1].n1); + else + { + t = fold_convert (TREE_TYPE (fd->loops[i + 1].v), + fd->loops[i + 1].n1); + tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1); + t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3); + t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2); + } } + else + t = fold_convert (TREE_TYPE (fd->loops[i + 1].v), + fd->loops[i + 1].n1); expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t); if (fd->loops[i + 1].m2) { @@ -6889,14 +6876,19 @@ expand_omp_simd (struct omp_region *regi gcc_assert (n2v == NULL_TREE); n2v = create_tmp_var (TREE_TYPE (fd->loops[i + 1].v)); } - t = fold_convert (TREE_TYPE (fd->loops[i + 1].v), - fd->loops[i + 1].n2); - tree t2 = fold_convert (TREE_TYPE (t), + tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v), fd->loops[i + 1 - fd->loops[i + 1].outer].v); - tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2); - t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3); - t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2); + if (POINTER_TYPE_P (TREE_TYPE (t2))) + t = fold_build_pointer_plus (t2, fd->loops[i + 1].n2); + else + { + t = fold_convert (TREE_TYPE (fd->loops[i + 1].v), + fd->loops[i + 1].n2); + tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2); + t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3); + t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2); + } expand_omp_build_assign (&gsi, n2v, t); } if (i + 2 == fd->collapse && n2var) @@ -6912,17 +6904,25 @@ expand_omp_simd (struct omp_region *regi tree t2 = fold_build2 (MINUS_EXPR, type, n2, fd->loop.v); if (fd->loops[i + 1].m1 || fd->loops[i + 1].m2) { + tree itype = TREE_TYPE (fd->loops[i].v); + if (POINTER_TYPE_P (itype)) + itype = signed_type_for (itype); t = build_int_cst (itype, (fd->loops[i + 1].cond_code == LT_EXPR ? -1 : 1)); t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, fd->loops[i + 1].step), t); - if (fd->loops[i + 1].m2) - t = fold_build2 (PLUS_EXPR, itype, t, n2v); - else + if (fd->loops[i + 1].m2 == NULL_TREE) t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, fd->loops[i + 1].n2)); + else if (POINTER_TYPE_P (TREE_TYPE (n2v))) + { + t = fold_build_pointer_plus (n2v, t); + t = fold_convert (itype, t); + } + else + t = fold_build2 (PLUS_EXPR, itype, t, n2v); t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, fd->loops[i + 1].v)); tree step = fold_convert (itype, fd->loops[i + 1].step); --- libgomp/testsuite/libgomp.c-c++-common/pr106449.c.jj 2022-07-28 11:35:50.276558893 +0200 +++ libgomp/testsuite/libgomp.c-c++-common/pr106449.c 2022-07-28 12:03:27.495268905 +0200 @@ -0,0 +1,62 @@ +/* PR middle-end/106449 */ +/* { dg-do run } */ + +void +foo (void) +{ + int a[1024], *b[65536]; + int *p, *q, **r = &b[0], i; + #pragma omp simd collapse(2) linear(r : 2) + for (p = &a[0]; p < &a[512]; p++) + for (q = p + 64; q < p + 128; q++) + { + *r++ = p; + *r++ = q; + } + for (i = 0; i < 32768; i++) + if (b[2 * i] != &a[i / 64] || b[2 * i + 1] != &a[(i / 64) + 64 + (i % 64)]) + __builtin_abort (); +} + +void +bar (int n, int m) +{ + int a[1024], *b[65536]; + int *p, *q, **r = &b[0], i; + #pragma omp parallel for simd collapse(2) linear(r : 2) + for (p = &a[0]; p < &a[512]; p++) + for (q = p + n; q < p + m; q++) + { + *r++ = p; + *r++ = q; + } + for (i = 0; i < 32768; i++) + if (b[2 * i] != &a[i / 64] || b[2 * i + 1] != &a[(i / 64) + 64 + (i % 64)]) + __builtin_abort (); +} + +void +baz (int n, int m) +{ + int a[1024], *b[8192]; + int *p, *q, **r = &b[0], i; + #pragma omp parallel for simd collapse(2) linear(r : 2) + for (p = &a[0]; p < &a[512]; p += 4) + for (q = p + n; q < p + m; q += 2) + { + *r++ = p; + *r++ = q; + } + for (i = 0; i < 4096; i++) + if (b[2 * i] != &a[(i / 32) * 4] || b[2 * i + 1] != &a[(i / 32) * 4 + 64 + (i % 32) * 2]) + __builtin_abort (); +} + +int +main () +{ + foo (); + bar (64, 128); + baz (64, 128); + return 0; +}