From patchwork Wed Sep 18 13:36:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 97645 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E909A3858414 for ; Wed, 18 Sep 2024 13:37:25 +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 6DE2E3858C66 for ; Wed, 18 Sep 2024 13:36:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DE2E3858C66 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 6DE2E3858C66 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=1726666613; cv=none; b=ZmqyyLfEp0A04YyT69WYmirnZSotjOO9iHaZYCzA5VRwI16NrLkfcvfLoXwKzotIKwdMB2FTFVAclv0QhehYIrHXHtVSLjiBZTGIZQvSESDnM4gKl33AHd+t34tsGy/fFWJNpx+t4s7LvryHSZIYdr8Mdy8vlGBicp8+EHBVysA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726666613; c=relaxed/simple; bh=TWTmCRe9Tdi914gPR7MSU8I37Kuc0PIoczv1r0GPWw4=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=FBv41lG2cCWXSchfh6s+lzuSPUelcG/54xHa28GwAPGtEnYGr1e/MFlOU3DQy832uDpCWphnnoVOfWYBQvW1ID/wHUwEiNi+sQOvSdfjM77F48FtH8X15/iuaK0+x/hwbXuaXvwjUbpxP8Xq/fACGuCi1NlEPQSR1K54P3KokgY= 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 71ECB1FC04 for ; Wed, 18 Sep 2024 13:36:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1726666609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=bVXpxumnZB3QDWdOlrmSws83/AQydDeBdB0BbU9oggU=; b=SYRa/FM4zc+S7fIY4LtXX7Tly3lNQ8eaMeuULCpuhq75+F5LNZExmbFd2la20jU7lbKXY9 0H72oXxuXTFuIIbNwCWlIJQ1K5N6nmBTLFdwbylTi2PNJxLUoSA+sjoB9uuhTWH9/DNr23 jWf9wIzrk4IcruRB1EmSPAfjWRtLS1w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1726666609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=bVXpxumnZB3QDWdOlrmSws83/AQydDeBdB0BbU9oggU=; b=QFoMVVYnj+UR3IsEg3vqnWTQMM/gljoyCuvQJ3TRuC8/YR9H+ZJINdqpF5t3t9dRU0rww1 +C9vIcsp5zFF/FCw== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1726666609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=bVXpxumnZB3QDWdOlrmSws83/AQydDeBdB0BbU9oggU=; b=SYRa/FM4zc+S7fIY4LtXX7Tly3lNQ8eaMeuULCpuhq75+F5LNZExmbFd2la20jU7lbKXY9 0H72oXxuXTFuIIbNwCWlIJQ1K5N6nmBTLFdwbylTi2PNJxLUoSA+sjoB9uuhTWH9/DNr23 jWf9wIzrk4IcruRB1EmSPAfjWRtLS1w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1726666609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=bVXpxumnZB3QDWdOlrmSws83/AQydDeBdB0BbU9oggU=; b=QFoMVVYnj+UR3IsEg3vqnWTQMM/gljoyCuvQJ3TRuC8/YR9H+ZJINdqpF5t3t9dRU0rww1 +C9vIcsp5zFF/FCw== 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 5817113508 for ; Wed, 18 Sep 2024 13:36:49 +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 FmzlE3HX6mZ7OwAAD6G6ig (envelope-from ) for ; Wed, 18 Sep 2024 13:36:49 +0000 Date: Wed, 18 Sep 2024 15:36:40 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH][v2] Fall back to single-lane SLP before falling back to no SLP MIME-Version: 1.0 Message-Id: <20240918133649.5817113508@imap1.dmz-prg2.suse.org> X-Spam-Level: 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)[]; RCPT_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+]; MISSING_XM_UA(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[tree-vect-loop.cc:url, imap1.dmz-prg2.suse.org:helo, imap1.dmz-prg2.suse.org:mid] X-Spam-Score: -4.30 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~patchwork=sourceware.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 on x86_64-unknown-linux-gnu, testing in progreess. v2 fixes issues that made the earlier version non-functional (oops). Richard. * 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 | 49 ++++++++++++++++++++++++------------------- gcc/tree-vect-slp.cc | 43 +++++++++++++++++++++---------------- gcc/tree-vectorizer.h | 2 +- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 62c7f90779f..316a5bad628 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); @@ -2854,18 +2854,23 @@ vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal, vect_compute_single_scalar_iteration_cost (loop_vinfo); poly_uint64 saved_vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo); + bool saved_can_use_partial_vectors_p + = LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo); + + /* This is the point where we can re-start analysis with SLP forced off. */ +start_over: if (slp) { /* 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; /* If there are any SLP instances mark them as pure_slp. */ - slp = vect_make_slp_decision (loop_vinfo); - if (slp) + if (vect_make_slp_decision (loop_vinfo)) { /* Find stmts that need to be both vectorized and SLPed. */ vect_detect_hybrid_slp (loop_vinfo); @@ -2881,16 +2886,10 @@ vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal, } } - bool saved_can_use_partial_vectors_p - = LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo); - /* We don't expect to have to roll back to anything other than an empty set of rgroups. */ gcc_assert (LOOP_VINFO_MASKS (loop_vinfo).is_empty ()); - /* This is the point where we can re-start analysis with SLP forced off. */ -start_over: - /* Apply the suggested unrolling factor, this was determined by the backend during finish_cost the first time we ran the analyzis for this vector mode. */ @@ -3199,15 +3198,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. */ @@ -3252,11 +3250,18 @@ again: } if (dump_enabled_p ()) - dump_printf_loc (MSG_NOTE, vect_location, - "re-trying with SLP disabled\n"); + { + if (slp) + dump_printf_loc (MSG_NOTE, vect_location, + "re-trying with single-lane SLP\n"); + else + 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. */ @@ -3401,7 +3406,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 b4726140c45..8dc9deabec3 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -3493,7 +3493,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. */ @@ -3689,7 +3689,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) @@ -3896,7 +3896,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. */ @@ -3909,14 +3909,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; } } @@ -4549,7 +4549,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; @@ -4566,7 +4567,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) @@ -4586,7 +4588,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); } } @@ -4603,7 +4605,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; @@ -4619,9 +4622,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; @@ -4661,7 +4666,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 { @@ -4675,7 +4681,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); } } } @@ -4688,7 +4695,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 (); @@ -4704,7 +4711,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 (); } @@ -4736,7 +4743,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); } } } @@ -8939,7 +8946,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 *);