From patchwork Thu Jun 13 13:43:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1947423 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=xWSl+35q; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=ouotBxSD; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=xWSl+35q; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=ouotBxSD; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W0Ntw3bdwz20Xd for ; Thu, 13 Jun 2024 23:44:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF3BF3882155 for ; Thu, 13 Jun 2024 13:44:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 6E443388264D for ; Thu, 13 Jun 2024 13:43:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E443388264D 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 6E443388264D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718286230; cv=none; b=nus9wAwBkxMP1k+IG/jFoyLWm4B9ZiLSaePf5rkv2nv1O1rf2shLB/GSyAcpkx9lRjbMCP/2JhguiCPkUQt69t+xgir6X/BtDTrpohqm+mW8UripfvfXMsV+LTxBTRd7JZNbOCBv50ne1CQGt7xHEUr2zZtKwGKluAaL3gApl/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718286230; c=relaxed/simple; bh=zX/wVLnPxQaahTt+PUGl42qWJ1T4MjqBM/Wf1fzQanc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=Lf9IG/if2ShAv9mWMLWg3YreWXtTSVQrAoprSRB2KwBWRgFdYSXyTXgTBccIPYDcA9pwiu9doTN5qTskI6usjDDVgO4X0WEGymj47IMAxzlOzyL8mnXvMhnK4/xm6vfAu5KgK/5JTwxuC+9ykvBftKIl2FadzxAK1FCrQUViuto= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from murzim.nue2.suse.org (unknown [10.168.4.243]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 69DD15D420 for ; Thu, 13 Jun 2024 13:43:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1718286227; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=7z3LrB3zXfM83pfpbKyFj6SsrPudjnlNhcykgdnQ7Bc=; b=xWSl+35qVs0afGH9ebim/j0Cg/0TAHZ1/Dq/3hxi8u0GFOWU6m2+a2qIRKB1qefPtrUinu ZMmv0ZrLVLEnA+YXipOaBNyEoU8HiMEWxSm6dJ/Q/OrS8MZOHIUhbq9FOkiUlrDJpG0ZPQ o/fs2fZ4gjo+6Mb2ouTIQQdEifW1Z0U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1718286227; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=7z3LrB3zXfM83pfpbKyFj6SsrPudjnlNhcykgdnQ7Bc=; b=ouotBxSDCWevwAtYMAU2rURCnvfFaUGda5WbWR2T11HJxGFwXOpuqBsvhQZc9VsU6vzLJY jnLZov8weev+l2CQ== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1718286227; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=7z3LrB3zXfM83pfpbKyFj6SsrPudjnlNhcykgdnQ7Bc=; b=xWSl+35qVs0afGH9ebim/j0Cg/0TAHZ1/Dq/3hxi8u0GFOWU6m2+a2qIRKB1qefPtrUinu ZMmv0ZrLVLEnA+YXipOaBNyEoU8HiMEWxSm6dJ/Q/OrS8MZOHIUhbq9FOkiUlrDJpG0ZPQ o/fs2fZ4gjo+6Mb2ouTIQQdEifW1Z0U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1718286227; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=7z3LrB3zXfM83pfpbKyFj6SsrPudjnlNhcykgdnQ7Bc=; b=ouotBxSDCWevwAtYMAU2rURCnvfFaUGda5WbWR2T11HJxGFwXOpuqBsvhQZc9VsU6vzLJY jnLZov8weev+l2CQ== Date: Thu, 13 Jun 2024 15:43:47 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Support single def-use cycle optimization for SLP reduction vectorization MIME-Version: 1.0 X-Spamd-Result: default: False [-1.23 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_LONG(-0.60)[-0.603]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_SHORT(-0.03)[-0.148]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_ZERO(0.00)[0]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[] X-Spam-Score: -1.23 X-Spam-Level: X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, 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.30 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 Message-Id: <20240613134438.BF3BF3882155@sourceware.org> We can at least mimic single def-use cycle optimization when doing single-lane SLP reductions and that's required to avoid regressing compared to non-SLP. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. I've noticed this when looking at PR115438 but the fix doesn't help there. * tree-vect-loop.cc (vectorizable_reduction): Allow single-def-use cycles with SLP. (vect_transform_reduction): Handle SLP single def-use cycles. (vect_transform_cycle_phi): Likewise. * gcc.dg/vect/slp-reduc-12.c: New testcase. --- gcc/testsuite/gcc.dg/vect/slp-reduc-12.c | 18 +++++++++ gcc/tree-vect-loop.cc | 48 +++++++++++++++--------- 2 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/slp-reduc-12.c diff --git a/gcc/testsuite/gcc.dg/vect/slp-reduc-12.c b/gcc/testsuite/gcc.dg/vect/slp-reduc-12.c new file mode 100644 index 00000000000..625f8097c54 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/slp-reduc-12.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_double } */ +/* { dg-require-effective-target vect_int } */ +/* { dg-require-effective-target vect_hw_misalign } */ +/* { dg-additional-options "-Ofast" } */ + +double foo (double *x, int * __restrict a, int n) +{ + double r = 0.; + for (int i = 0; i < n; ++i) + { + a[i] = a[i] + i; + r += x[i]; + } + return r; +} + +/* { dg-final { scan-tree-dump "using single def-use cycle for reduction" "vect" } } */ diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 8f5ac426790..b0a3f77c1ca 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -8337,7 +8337,11 @@ vectorizable_reduction (loop_vec_info loop_vinfo, participating. When unrolling we want each unrolled iteration to have its own reduction accumulator since one of the main goals of unrolling a reduction is to reduce the aggregate loop-carried latency. */ - if (ncopies > 1 + if ((ncopies > 1 + || (slp_node + && !REDUC_GROUP_FIRST_ELEMENT (stmt_info) + && SLP_TREE_LANES (slp_node) == 1 + && vect_get_num_copies (loop_vinfo, vectype_in) > 1)) && (STMT_VINFO_RELEVANT (stmt_info) <= vect_used_only_live) && reduc_chain_length == 1 && loop_vinfo->suggested_unroll_factor == 1) @@ -8390,6 +8394,10 @@ vectorizable_reduction (loop_vec_info loop_vinfo, single_defuse_cycle = false; } } + if (dump_enabled_p () && single_defuse_cycle) + dump_printf_loc (MSG_NOTE, vect_location, + "using single def-use cycle for reduction by reducing " + "multiple vectors to one in the loop body\n"); STMT_VINFO_FORCE_SINGLE_CYCLE (reduc_info) = single_defuse_cycle; /* If the reduction stmt is one of the patterns that have lane @@ -8545,9 +8553,8 @@ vect_transform_reduction (loop_vec_info loop_vinfo, { tree vectype_out = STMT_VINFO_VECTYPE (stmt_info); class loop *loop = LOOP_VINFO_LOOP (loop_vinfo); - int i; - int ncopies; - int vec_num; + unsigned ncopies; + unsigned vec_num; stmt_vec_info reduc_info = info_for_reduction (loop_vinfo, stmt_info); gcc_assert (reduc_info->is_reduc_info); @@ -8594,7 +8601,6 @@ vect_transform_reduction (loop_vec_info loop_vinfo, auto_vec vec_oprnds0; auto_vec vec_oprnds1; auto_vec vec_oprnds2; - tree def0; if (dump_enabled_p ()) dump_printf_loc (MSG_NOTE, vect_location, "transform reduction.\n"); @@ -8669,20 +8675,24 @@ vect_transform_reduction (loop_vec_info loop_vinfo, definition. */ if (single_defuse_cycle) { - gcc_assert (!slp_node); - vect_get_vec_defs_for_operand (loop_vinfo, stmt_info, 1, - op.ops[reduc_index], - reduc_index == 0 ? &vec_oprnds0 - : (reduc_index == 1 ? &vec_oprnds1 - : &vec_oprnds2)); + vect_get_vec_defs (loop_vinfo, stmt_info, slp_node, 1, + !(single_defuse_cycle && reduc_index == 0) + ? NULL_TREE : op.ops[0], &vec_oprnds0, + !(single_defuse_cycle && reduc_index == 1) + ? NULL_TREE : op.ops[1], &vec_oprnds1, + op.num_ops == 3 + && (single_defuse_cycle && reduc_index == 2) + ? op.ops[2] : NULL_TREE, &vec_oprnds2); } bool emulated_mixed_dot_prod = vect_is_emulated_mixed_dot_prod (stmt_info); - FOR_EACH_VEC_ELT (vec_oprnds0, i, def0) + unsigned num = (reduc_index == 0 + ? vec_oprnds1.length () : vec_oprnds0.length ()); + for (unsigned i = 0; i < num; ++i) { gimple *new_stmt; - tree vop[3] = { def0, vec_oprnds1[i], NULL_TREE }; + tree vop[3] = { vec_oprnds0[i], vec_oprnds1[i], NULL_TREE }; if (masked_loop_p && !mask_by_cond_expr) { /* No conditional ifns have been defined for dot-product yet. */ @@ -8737,10 +8747,7 @@ vect_transform_reduction (loop_vec_info loop_vinfo, vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt, gsi); } - if (slp_node) - slp_node->push_vec_def (new_stmt); - else if (single_defuse_cycle - && i < ncopies - 1) + if (single_defuse_cycle && i < num - 1) { if (reduc_index == 0) vec_oprnds0.safe_push (gimple_get_lhs (new_stmt)); @@ -8749,6 +8756,8 @@ vect_transform_reduction (loop_vec_info loop_vinfo, else if (reduc_index == 2) vec_oprnds2.safe_push (gimple_get_lhs (new_stmt)); } + else if (slp_node) + slp_node->push_vec_def (new_stmt); else STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt); } @@ -8812,7 +8821,10 @@ vect_transform_cycle_phi (loop_vec_info loop_vinfo, /* Check whether we should use a single PHI node and accumulate vectors to one before the backedge. */ if (STMT_VINFO_FORCE_SINGLE_CYCLE (reduc_info)) - ncopies = 1; + { + ncopies = 1; + vec_num = 1; + } /* Create the destination vector */ gphi *phi = as_a (stmt_info->stmt);