From patchwork Wed Sep 18 11:22:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1986760 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=oBzRu7eH; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=M2darObs; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=HbrG2yvw; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=2t3acEQG; 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 4X7x9L2xCtz1y1t for ; Wed, 18 Sep 2024 21:23:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 972DC3858414 for ; Wed, 18 Sep 2024 11:23:30 +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 [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 6C5FF3858D39 for ; Wed, 18 Sep 2024 11:23:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C5FF3858D39 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 6C5FF3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726658587; cv=none; b=pdl1FAiDsGztQF3t4Xc38Sq8h1MhmJgqXmdHhs+6WbZE/tuf1xoqQRCBgsE6jCUK8hWbPUaky/0fT8DtOlmSfG5C39hmHPxjwWjQrxBF0YRZSAVokVMOvg7U+eMQYQhC229bRnitszuTSB5naOseFKMImdIIkSb+usnkzqAt1Yo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726658587; c=relaxed/simple; bh=xyclf6Y5idr8tu/e8dPa0YIU0hUIwBAAiCxZgx566S0=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=QQ6+s7yLeGBWOMyOWE/dnrWpdydnYavhKVb+JQvDscDkQBl7YsxLt/kocB51tL6AbuKL/AOtBhtgnKecfHk8xABcfPGTyZ4perRKHPoUopTWubsnXlIZ2tUj0hF7uTF1MnU7yvi3eKe8S/fsJcwget4IMKHebwm+uji78MwTncQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 4DD871FBCD; Wed, 18 Sep 2024 11:23:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1726658583; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=MG0e/y2nruJOr6oiAjM7dNMZ2/0PE0WKNFNLQpUJFhw=; b=oBzRu7eHpDbR/K3RqSy+tnrWXWefkVFJbw++g9EJXB5YKnZxmd3UJxpZ6F1XVH4aywOYkn mvCKG7jh/GOPAPAgTRaevDOGxlPTcYPRqRgwTbHA+XTpZpd9CrS2D3BegApXy+fwWyCIwY CxIe2wG2xk/MfvjAMlHs4dk+clfyHEI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1726658583; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=MG0e/y2nruJOr6oiAjM7dNMZ2/0PE0WKNFNLQpUJFhw=; b=M2darObsZRDcDrgXYYv065+Kh5l3RGWcqgj3Z/CwLAWfh/Zl9Z/PxzGE+lUN8AaYBXMqdr 9cxwg5ltCOgUh0Ag== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1726658582; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=MG0e/y2nruJOr6oiAjM7dNMZ2/0PE0WKNFNLQpUJFhw=; b=HbrG2yvw5ZQ3K65F0aqo5jVxguJK88RRJvLr/L0KeldAOHNSDKolScht/o5CqtFeZKDJlb sOTtm7lZkeEu/Ac3ygluT4snIqV7qb47lvoD5r7R/MyHMADg4BPkmlVjCitIMRMpuerPX8 5f2+cusuPoIhE9jOCb0TOMIx57KiVPg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1726658582; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=MG0e/y2nruJOr6oiAjM7dNMZ2/0PE0WKNFNLQpUJFhw=; b=2t3acEQGI6GM9smf0nbyrRbFFZbGWo1+R7GQbGxRepB8Mbc4q9adnkRjL9OWHEC1sWYpR+ IIUi5j44+NA31oDw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 2D52013508; Wed, 18 Sep 2024 11:23:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id tfeBCRa46mboEQAAD6G6ig (envelope-from ); Wed, 18 Sep 2024 11:23:02 +0000 Date: Wed, 18 Sep 2024 13:22:53 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: RISC-V Subject: [PATCH] Fall back to single-lane SLP before falling back to no SLP MIME-Version: 1.0 Message-Id: <20240918112302.2D52013508@imap1.dmz-prg2.suse.org> X-Spam-Score: -4.30 X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[] X-Spam-Level: X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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~incoming=patchwork.ozlabs.org@gcc.gnu.org The following changes the fallback to disable SLP when any of the discovered SLP instances failed to pass vectorization checking into a fallback that emulates what no SLP would do with SLP - force single-lane discovery for all instances. The patch does not remove the final fallback to disable SLP but it reduces the fallout from failing vectorization when any non-SLP stmt survives analysis. Bootstrapped and tested on x86_64-unknown-linux-gnu. I'm watching CI results but do not really expect to see big improvements besides of when forcing SLP as with the recently posted "Testing: fail vectorization when not using SLP" patch. * tree-vectorizer.h (vect_analyze_slp): Add force_single_lane parameter. * tree-vect-slp.cc (vect_analyze_slp_instance): Remove defaulting of force_single_lane. (vect_build_slp_instance): Likewise. Pass down appropriate force_single_lane. (vect_analyze_slp): Add force_sigle_lane parameter and pass it down appropriately. (vect_slp_analyze_bb_1): Always do multi-lane SLP. * tree-vect-loop.cc (vect_analyze_loop_2): Track two SLP modes and adjust accordingly. (vect_analyze_loop_1): Save the SLP mode when unrolling. --- gcc/tree-vect-loop.cc | 25 +++++++++++++------------ gcc/tree-vect-slp.cc | 43 +++++++++++++++++++++++++------------------ gcc/tree-vectorizer.h | 2 +- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 199d79029e4..8bf231e98ec 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -2718,7 +2718,7 @@ vect_determine_partial_vectors_and_peeling (loop_vec_info loop_vinfo) static opt_result vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal, unsigned *suggested_unroll_factor, - bool& slp_done_for_suggested_uf) + unsigned& slp_done_for_suggested_uf) { opt_result ok = opt_result::success (); int res; @@ -2787,11 +2787,11 @@ vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal, /* If the slp decision is false when suggested unroll factor is worked out, and we are applying suggested unroll factor, we can simply skip all slp related analyses this time. */ - bool slp = !applying_suggested_uf || slp_done_for_suggested_uf; + unsigned slp = !applying_suggested_uf ? 2 : slp_done_for_suggested_uf; /* Classify all cross-iteration scalar data-flow cycles. Cross-iteration cycles caused by virtual phis are analyzed separately. */ - vect_analyze_scalar_cycles (loop_vinfo, slp); + vect_analyze_scalar_cycles (loop_vinfo, slp == 2); vect_pattern_recog (loop_vinfo); @@ -2859,7 +2859,8 @@ vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal, { /* Check the SLP opportunities in the loop, analyze and build SLP trees. */ - ok = vect_analyze_slp (loop_vinfo, LOOP_VINFO_N_STMTS (loop_vinfo)); + ok = vect_analyze_slp (loop_vinfo, LOOP_VINFO_N_STMTS (loop_vinfo), + slp == 1); if (!ok) return ok; @@ -3212,15 +3213,14 @@ again: /* Ensure that "ok" is false (with an opt_problem if dumping is enabled). */ gcc_assert (!ok); - /* Try again with SLP forced off but if we didn't do any SLP there is + /* Try again with SLP degraded but if we didn't do any SLP there is no point in re-trying. */ if (!slp) return ok; - /* If the slp decision is true when suggested unroll factor is worked - out, and we are applying suggested unroll factor, we don't need to - re-try any more. */ - if (applying_suggested_uf && slp_done_for_suggested_uf) + /* If we are applying suggested unroll factor, we don't need to + re-try any more as we want to keep the SLP mode fixed. */ + if (applying_suggested_uf) return ok; /* If there are reduction chains re-trying will fail anyway. */ @@ -3268,8 +3268,9 @@ again: dump_printf_loc (MSG_NOTE, vect_location, "re-trying with SLP disabled\n"); - /* Roll back state appropriately. No SLP this time. */ - slp = false; + /* Roll back state appropriately. Degrade SLP this time. From multi- + to single-lane to disabled. */ + --slp; /* Restore vectorization factor as it were without SLP. */ LOOP_VINFO_VECT_FACTOR (loop_vinfo) = saved_vectorization_factor; /* Free the SLP instances. */ @@ -3414,7 +3415,7 @@ vect_analyze_loop_1 (class loop *loop, vec_info_shared *shared, machine_mode vector_mode = vector_modes[mode_i]; loop_vinfo->vector_mode = vector_mode; unsigned int suggested_unroll_factor = 1; - bool slp_done_for_suggested_uf = false; + unsigned slp_done_for_suggested_uf = 0; /* Run the main analysis. */ opt_result res = vect_analyze_loop_2 (loop_vinfo, fatal, diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index 4fcb9e2fa2b..c8af4d320eb 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -3488,7 +3488,7 @@ vect_analyze_slp_instance (vec_info *vinfo, scalar_stmts_to_slp_tree_map_t *bst_map, stmt_vec_info stmt_info, slp_instance_kind kind, unsigned max_tree_size, unsigned *limit, - bool force_single_lane = false); + bool force_single_lane); /* Build an interleaving scheme for the store sources RHS_NODES from SCALAR_STMTS. */ @@ -3684,7 +3684,7 @@ vect_build_slp_instance (vec_info *vinfo, scalar_stmts_to_slp_tree_map_t *bst_map, /* ??? We need stmt_info for group splitting. */ stmt_vec_info stmt_info_, - bool force_single_lane = false) + bool force_single_lane) { /* If there's no budget left bail out early. */ if (*limit == 0) @@ -3891,7 +3891,7 @@ vect_build_slp_instance (vec_info *vinfo, group1_size); bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info, kind, max_tree_size, - limit); + limit, false); /* Split the rest at the failure point and possibly re-analyze the remaining matching part if it has at least two lanes. */ @@ -3904,14 +3904,14 @@ vect_build_slp_instance (vec_info *vinfo, if (i - group1_size > 1) res |= vect_analyze_slp_instance (vinfo, bst_map, rest2, kind, max_tree_size, - limit); + limit, false); } /* Re-analyze the non-matching tail if it has at least two lanes. */ if (i + 1 < group_size) res |= vect_analyze_slp_instance (vinfo, bst_map, rest, kind, max_tree_size, - limit); + limit, false); return res; } } @@ -4544,7 +4544,8 @@ vect_lower_load_permutations (loop_vec_info loop_vinfo, trees of packed scalar stmts if SLP is possible. */ opt_result -vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) +vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size, + bool force_single_lane) { loop_vec_info loop_vinfo = dyn_cast (vinfo); unsigned int i; @@ -4561,7 +4562,8 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) /* Find SLP sequences starting from groups of grouped stores. */ FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element) vect_analyze_slp_instance (vinfo, bst_map, first_element, - slp_inst_kind_store, max_tree_size, &limit); + slp_inst_kind_store, max_tree_size, &limit, + force_single_lane); /* For loops also start SLP discovery from non-grouped stores. */ if (loop_vinfo) @@ -4581,7 +4583,7 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) stmts.quick_push (stmt_info); vect_build_slp_instance (vinfo, slp_inst_kind_store, stmts, roots, remain, max_tree_size, - &limit, bst_map, NULL); + &limit, bst_map, NULL, force_single_lane); } } @@ -4598,7 +4600,8 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) bb_vinfo->roots[i].stmts, bb_vinfo->roots[i].roots, bb_vinfo->roots[i].remain, - max_tree_size, &limit, bst_map, NULL)) + max_tree_size, &limit, bst_map, NULL, + false)) { bb_vinfo->roots[i].stmts = vNULL; bb_vinfo->roots[i].roots = vNULL; @@ -4614,9 +4617,11 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) if (! STMT_VINFO_RELEVANT_P (first_element) && ! STMT_VINFO_LIVE_P (first_element)) ; - else if (! vect_analyze_slp_instance (vinfo, bst_map, first_element, - slp_inst_kind_reduc_chain, - max_tree_size, &limit)) + else if (force_single_lane + || ! vect_analyze_slp_instance (vinfo, bst_map, first_element, + slp_inst_kind_reduc_chain, + max_tree_size, &limit, + force_single_lane)) { /* Dissolve reduction chain group. */ stmt_vec_info vinfo = first_element; @@ -4656,7 +4661,8 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) { /* Do not discover SLP reductions combining lane-reducing ops, that will fail later. */ - if (!lane_reducing_stmt_p (STMT_VINFO_STMT (next_info))) + if (!force_single_lane + && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info))) scalar_stmts.quick_push (next_info); else { @@ -4670,7 +4676,8 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) slp_inst_kind_reduc_group, stmts, roots, remain, max_tree_size, &limit, - bst_map, NULL); + bst_map, NULL, + force_single_lane); } } } @@ -4683,7 +4690,7 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) slp_inst_kind_reduc_group, scalar_stmts, roots, remain, max_tree_size, &limit, bst_map, - NULL)) + NULL, force_single_lane)) { if (scalar_stmts.length () <= 1) scalar_stmts.release (); @@ -4699,7 +4706,7 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) slp_inst_kind_reduc_group, stmts, roots, remain, max_tree_size, &limit, - bst_map, NULL); + bst_map, NULL, force_single_lane); } saved_stmts.release (); } @@ -4731,7 +4738,7 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) slp_inst_kind_reduc_group, stmts, roots, remain, max_tree_size, &limit, - bst_map, NULL); + bst_map, NULL, force_single_lane); } } } @@ -8934,7 +8941,7 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal, /* Check the SLP opportunities in the basic block, analyze and build SLP trees. */ - if (!vect_analyze_slp (bb_vinfo, n_stmts)) + if (!vect_analyze_slp (bb_vinfo, n_stmts, false)) { if (dump_enabled_p ()) { diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h index 699ae9e33ba..53105f9292f 100644 --- a/gcc/tree-vectorizer.h +++ b/gcc/tree-vectorizer.h @@ -2538,7 +2538,7 @@ extern bool vect_transform_slp_perm_load (vec_info *, slp_tree, const vec unsigned * = nullptr, bool = false); extern bool vect_slp_analyze_operations (vec_info *); extern void vect_schedule_slp (vec_info *, const vec &); -extern opt_result vect_analyze_slp (vec_info *, unsigned); +extern opt_result vect_analyze_slp (vec_info *, unsigned, bool); extern bool vect_make_slp_decision (loop_vec_info); extern void vect_detect_hybrid_slp (loop_vec_info); extern void vect_optimize_slp (vec_info *);