From patchwork Thu Oct 19 11:47:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 155458 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp322876vqb; Thu, 19 Oct 2023 04:48:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFp4UJj7XuMhsbqOKoeHXjRbCpq1hrMPgVecUZmSmtA+5Mt4j6Jzo779XNaj75MWTboc60a X-Received: by 2002:a05:6122:4592:b0:49e:1eca:f849 with SMTP id de18-20020a056122459200b0049e1ecaf849mr1849249vkb.13.1697716084268; Thu, 19 Oct 2023 04:48:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697716084; cv=pass; d=google.com; s=arc-20160816; b=z5ly1c4fZfMkPgzePVwgImCMHpR4E8NvbJuKzLiF+1HDkK/1RGmFklt6Wcq9G/b/4k dFPV7F6Tclbfd32WRyz2aRh350e0pH+8xYa2PBy7TrDbXj9oMeBd7G97JFHgyBa7YkPj AxsCHYjANEHaTIe0lst8m9il9hWZpxyJjB9BeQNGFupnq9BI2M65mfl1iLuA5vId+bkq CixPnWXedvp3O/IUw9BQpDWMokaDYcrMWgUCZCJ6tapCV/UXKJ5yp+mfzfH3OWJmTPxK m16ohYQH4EkgiJCXIaUsX19YgQjUwPcO/KhTDJc3F0/Nar+jVouSwGqQI9wWW2M653Z9 0XVg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:to:from:date:arc-filter:dmarc-filter :delivered-to; bh=uyAcYT3aRw8c5r7qwrpbwaqEJ8ayzxtGgML0ed/9M/s=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=xYGA4Her/u4ZYvtz3DBdsEreF39eYjWgHqNzaKvy9tmpHhU774epwI+2QhEZvWUwEc c/1Ofp3CbxKjWlh6eBnrLawEqlGYTKlLGxFKHpf6d5xVkKa00o5kr+V0eLod64VrcxIh c52aR7yqa8gkqRHoMg5hHmGoQhveQrbnGjPLIwedHoior3y375m8/pKTfVIza77ejhCP MC0cRKJUVKVOzrNgJ8dh87zht2iPzvog9JVY8u/jzA4WNbF5OoGk4fSPSvdV+ObPIw11 CsvHD/e6u8tV83g6CeIKujNi+g8IBAQVIGsbYJ1KxXnERiIIPn2xFa9H6hCjfOXP0Fgz Qw1w== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l8-20020a05622a174800b004188c21b6f8si1428185qtk.192.2023.10.19.04.48.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 04:48:04 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 055643858438 for ; Thu, 19 Oct 2023 11:48:04 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id E2E90385771B for ; Thu, 19 Oct 2023 11:47:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2E90385771B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E2E90385771B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.28 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697716038; cv=none; b=B79keNWli4wWW4UhNmyVwWJVImOBMUE9bDJLlUF2DhuTtGb1AOQG3O9wJoYfQT7szFMEKZa3jy6Ze0iGuCfseULqiyUDqfx5e+4dnl4ELwbAE2/t7B5UT5A+5AIugrnCSSilLs3YQnK7n6DAbVICyqEi6WLQrj6oImP4812YPYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697716038; c=relaxed/simple; bh=cI83B/gwMYwGqkOM8pGcmUg9soMYGqX1wAo14wfUngc=; h=Date:From:To:Subject:MIME-Version; b=pMZFK9bLjG9qix4t5eeDECr82Gnf5ppnaUMcwwPKiQ1gA+wVlSppfoycquzXb6D6C44X5eh98zakyyKpysVybu9ZhxjsO0M6l6i98but9q+Qi1e6TdCQY6uO+RnwGjVtQkhAZyKVqzRg/64T3QRDRxBxsnV9V7p5NdM6Jk5fcqs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id A88CB21A4D for ; Thu, 19 Oct 2023 11:47:14 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 72FFC2C521 for ; Thu, 19 Oct 2023 11:47:14 +0000 (UTC) Date: Thu, 19 Oct 2023 11:47:14 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] tree-optimization/111131 - SLP for non-IFN gathers User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out1.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out1.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [-0.01 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; R_SPF_SOFTFAIL(0.60)[~all:c]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; VIOLATED_DIRECT_SPF(3.50)[]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; DMARC_NA(0.20)[suse.de]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -0.01 X-Rspamd-Queue-Id: A88CB21A4D X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, MISSING_MID, 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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231019114804.055643858438@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780184341087230330 X-GMAIL-MSGID: 1780184341087230330 The following implements SLP vectorization support for gathers without relying on IFNs being pattern detected (and supported by the target). That includes support for emulated gathers but also the legacy x86 builtin path. Bootstrapped and tested on x86_64-unknown-linux-gnu, will push. Richard. PR tree-optimization/111131 * tree-vect-loop.cc (update_epilogue_loop_vinfo): Make sure to update all gather/scatter stmt DRs, not only those that eventually got VMAT_GATHER_SCATTER set. * tree-vect-slp.cc (_slp_oprnd_info::first_gs_info): Add. (vect_get_and_check_slp_defs): Handle gathers/scatters, adding the offset as SLP operand and comparing base and scale. (vect_build_slp_tree_1): Handle gathers. (vect_build_slp_tree_2): Likewise. * gcc.dg/vect/vect-gather-1.c: Now expected to vectorize everywhere. * gcc.dg/vect/vect-gather-2.c: Expected to not SLP anywhere. Massage the scale case to more reliably produce a different one. Scan for the specific messages. * gcc.dg/vect/vect-gather-3.c: Masked gather is also supported for AVX2, but not emulated. * gcc.dg/vect/vect-gather-4.c: Expected to not SLP anywhere. Massage to more properly ensure this. * gcc.dg/vect/tsvc/vect-tsvc-s353.c: Expect to vectorize everywhere. --- .../gcc.dg/vect/tsvc/vect-tsvc-s353.c | 2 +- gcc/testsuite/gcc.dg/vect/vect-gather-1.c | 2 +- gcc/testsuite/gcc.dg/vect/vect-gather-2.c | 13 ++++-- gcc/testsuite/gcc.dg/vect/vect-gather-3.c | 2 +- gcc/testsuite/gcc.dg/vect/vect-gather-4.c | 6 +-- gcc/tree-vect-loop.cc | 6 ++- gcc/tree-vect-slp.cc | 45 +++++++++++++++++-- 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/tsvc/vect-tsvc-s353.c b/gcc/testsuite/gcc.dg/vect/tsvc/vect-tsvc-s353.c index 98ba7522471..2c4fa3f5991 100644 --- a/gcc/testsuite/gcc.dg/vect/tsvc/vect-tsvc-s353.c +++ b/gcc/testsuite/gcc.dg/vect/tsvc/vect-tsvc-s353.c @@ -44,4 +44,4 @@ int main (int argc, char **argv) return 0; } -/* { dg-final { scan-tree-dump "vectorized 1 loops" "vect" { xfail { ! riscv_v } } } } */ +/* { dg-final { scan-tree-dump "vectorized 1 loops" "vect" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/vect-gather-1.c b/gcc/testsuite/gcc.dg/vect/vect-gather-1.c index e3bbf5c0bf8..5f6640d9ab6 100644 --- a/gcc/testsuite/gcc.dg/vect/vect-gather-1.c +++ b/gcc/testsuite/gcc.dg/vect/vect-gather-1.c @@ -58,4 +58,4 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump "Loop contains only SLP stmts" vect { target vect_gather_load_ifn } } } */ +/* { dg-final { scan-tree-dump "Loop contains only SLP stmts" vect } } */ diff --git a/gcc/testsuite/gcc.dg/vect/vect-gather-2.c b/gcc/testsuite/gcc.dg/vect/vect-gather-2.c index a1f6ba458a9..4c23b808333 100644 --- a/gcc/testsuite/gcc.dg/vect/vect-gather-2.c +++ b/gcc/testsuite/gcc.dg/vect/vect-gather-2.c @@ -8,6 +8,7 @@ f1 (int *restrict y, int *restrict x1, int *restrict x2, { for (int i = 0; i < N; ++i) { + /* Different base. */ y[i * 2] = x1[indices[i * 2]] + 1; y[i * 2 + 1] = x2[indices[i * 2 + 1]] + 2; } @@ -18,8 +19,9 @@ f2 (int *restrict y, int *restrict x, int *restrict indices) { for (int i = 0; i < N; ++i) { - y[i * 2] = x[indices[i * 2]] + 1; - y[i * 2 + 1] = x[indices[i * 2 + 1] * 2] + 2; + /* Different scale. */ + y[i * 2] = *(int *)((char *)x + (__UINTPTR_TYPE__)indices[i * 2] * 4) + 1; + y[i * 2 + 1] = *(int *)((char *)x + (__UINTPTR_TYPE__)indices[i * 2 + 1] * 2) + 2; } } @@ -28,9 +30,12 @@ f3 (int *restrict y, int *restrict x, int *restrict indices) { for (int i = 0; i < N; ++i) { + /* Different type. */ y[i * 2] = x[indices[i * 2]] + 1; - y[i * 2 + 1] = x[(unsigned int) indices[i * 2 + 1]] + 2; + y[i * 2 + 1] = x[((unsigned int *) indices)[i * 2 + 1]] + 2; } } -/* { dg-final { scan-tree-dump-not "Loop contains only SLP stmts" vect { target vect_gather_load_ifn } } } */ +/* { dg-final { scan-tree-dump-not "Loop contains only SLP stmts" vect } } */ +/* { dg-final { scan-tree-dump "different gather base" vect { target { ! vect_gather_load_ifn } } } } */ +/* { dg-final { scan-tree-dump "different gather scale" vect { target { ! vect_gather_load_ifn } } } } */ diff --git a/gcc/testsuite/gcc.dg/vect/vect-gather-3.c b/gcc/testsuite/gcc.dg/vect/vect-gather-3.c index adfef3bf407..30ba6789e03 100644 --- a/gcc/testsuite/gcc.dg/vect/vect-gather-3.c +++ b/gcc/testsuite/gcc.dg/vect/vect-gather-3.c @@ -62,4 +62,4 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump "Loop contains only SLP stmts" vect { target { vect_gather_load_ifn && vect_masked_load } } } } */ +/* { dg-final { scan-tree-dump "Loop contains only SLP stmts" vect { target { { vect_gather_load_ifn || avx2 } && vect_masked_load } } } } */ diff --git a/gcc/testsuite/gcc.dg/vect/vect-gather-4.c b/gcc/testsuite/gcc.dg/vect/vect-gather-4.c index ee2e4e4999a..1ce63e69199 100644 --- a/gcc/testsuite/gcc.dg/vect/vect-gather-4.c +++ b/gcc/testsuite/gcc.dg/vect/vect-gather-4.c @@ -39,10 +39,10 @@ f3 (int *restrict y, int *restrict x, int *restrict indices) y[i * 2] = (indices[i * 2] < N * 2 ? x[indices[i * 2]] + 1 : 1); - y[i * 2 + 1] = (indices[i * 2 + 1] < N * 2 - ? x[(unsigned int) indices[i * 2 + 1]] + 2 + y[i * 2 + 1] = (((unsigned int *)indices)[i * 2 + 1] < N * 2 + ? x[((unsigned int *) indices)[i * 2 + 1]] + 2 : 2); } } -/* { dg-final { scan-tree-dump-not "Loop contains only SLP stmts" vect { target vect_gather_load_ifn } } } */ +/* { dg-final { scan-tree-dump-not "Loop contains only SLP stmts" vect } } */ diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index ebab1953b9c..8877ebde246 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -11362,8 +11362,7 @@ update_epilogue_loop_vinfo (class loop *epilogue, tree advance) updated offset we set using ADVANCE. Instead we have to make sure the reference in the data references point to the corresponding copy of the original in the epilogue. */ - if (STMT_VINFO_MEMORY_ACCESS_TYPE (vect_stmt_to_vectorize (stmt_vinfo)) - == VMAT_GATHER_SCATTER) + if (STMT_VINFO_GATHER_SCATTER_P (vect_stmt_to_vectorize (stmt_vinfo))) { DR_REF (dr) = simplify_replace_tree (DR_REF (dr), NULL_TREE, NULL_TREE, @@ -11372,6 +11371,9 @@ update_epilogue_loop_vinfo (class loop *epilogue, tree advance) = simplify_replace_tree (DR_BASE_ADDRESS (dr), NULL_TREE, NULL_TREE, &find_in_mapping, &mapping); } + else + gcc_assert (STMT_VINFO_MEMORY_ACCESS_TYPE (vect_stmt_to_vectorize (stmt_vinfo)) + != VMAT_GATHER_SCATTER); DR_STMT (dr) = STMT_VINFO_STMT (stmt_vinfo); stmt_vinfo->dr_aux.stmt = stmt_vinfo; /* The vector size of the epilogue is smaller than that of the main loop diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index d081999a763..8efff2e912d 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -283,10 +283,11 @@ typedef struct _slp_oprnd_info vec ops; /* Information about the first statement, its vector def-type, type, the operand itself in case it's constant, and an indication if it's a pattern - stmt. */ + stmt and gather/scatter info. */ tree first_op_type; enum vect_def_type first_dt; bool any_pattern; + gather_scatter_info first_gs_info; } *slp_oprnd_info; @@ -609,6 +610,7 @@ vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char swap, unsigned int i, number_of_oprnds; enum vect_def_type dt = vect_uninitialized_def; slp_oprnd_info oprnd_info; + gather_scatter_info gs_info; unsigned int commutative_op = -1U; bool first = stmt_num == 0; @@ -660,6 +662,19 @@ vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char swap, oprnd_info = (*oprnds_info)[i]; + if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)) + { + gcc_assert (number_of_oprnds == 1); + if (!is_a (vinfo) + || !vect_check_gather_scatter (stmt_info, + as_a (vinfo), + first ? &oprnd_info->first_gs_info + : &gs_info)) + return -1; + + oprnd = first ? oprnd_info->first_gs_info.offset : gs_info.offset; + } + stmt_vec_info def_stmt_info; if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info)) { @@ -792,6 +807,25 @@ vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char swap, return 1; } + if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)) + { + if (!operand_equal_p (oprnd_info->first_gs_info.base, + gs_info.base)) + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "Build SLP failed: different gather base\n"); + return 1; + } + if (oprnd_info->first_gs_info.scale != gs_info.scale) + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "Build SLP failed: different gather scale\n"); + return 1; + } + } + /* Not first stmt of the group, check that the def-stmt/s match the def-stmt/s of the first stmt. Allow different definition types for reduction chains: the first stmt must be a @@ -1235,6 +1269,9 @@ vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap, || rhs_code == INDIRECT_REF || rhs_code == COMPONENT_REF || rhs_code == MEM_REF))) + || (ldst_p + && (STMT_VINFO_GATHER_SCATTER_P (stmt_info) + != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info))) || first_stmt_ldst_p != ldst_p || first_stmt_phi_p != phi_p) { @@ -1357,12 +1394,12 @@ vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap, if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)) && rhs_code != CFN_GATHER_LOAD && rhs_code != CFN_MASK_GATHER_LOAD + && !STMT_VINFO_GATHER_SCATTER_P (stmt_info) /* Not grouped loads are handled as externals for BB vectorization. For loop vectorization we can handle splats the same we handle single element interleaving. */ && (is_a (vinfo) - || stmt_info != first_stmt_info - || STMT_VINFO_GATHER_SCATTER_P (stmt_info))) + || stmt_info != first_stmt_info)) { /* Not grouped load. */ if (dump_enabled_p ()) @@ -1858,6 +1895,8 @@ vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node, gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD) || gimple_call_internal_p (stmt, IFN_GATHER_LOAD) || gimple_call_internal_p (stmt, IFN_MASK_GATHER_LOAD)); + else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)) + gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))); else { *max_nunits = this_max_nunits;