From patchwork Wed Jul 3 13:23:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1956239 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=gbpv1JcN; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=yPAHvZ/a; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=gbpv1JcN; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=yPAHvZ/a; 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 4WDgVN6NJxz1xqh for ; Wed, 3 Jul 2024 23:24:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 19C1A3861011 for ; Wed, 3 Jul 2024 13:24:27 +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 [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id BCFF13861022 for ; Wed, 3 Jul 2024 13:23:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BCFF13861022 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 BCFF13861022 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720013037; cv=none; b=PeSv3yHi1vuukSXN9TPhR1e62nz8V1lE47cQGANomlbJT+pVLZGLhiVcVVRBzlpmbhFS+t8gONMeOFlQ/5RgC72xj65dOL5WTA+FzzLsaEKrufshJVqp2FIvL+6LoN69sZSDJRtvFOw1ILA2SZx4+V8BKO8p1dWGmD5Af5CjYLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720013037; c=relaxed/simple; bh=rHxH1jotRLaAX5p6OWzes7PPznClrl1CERaYsrYxCMc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=Wb7j7fZSmHE/oQs2t6jvVLfPdLeUthd4n/4gA7WUsu/g3VsqZfJ0g1kd5iCDMBtHMOggSJxo0D0mnEZYutJ4q99I2+8798Zuwiu1tbvgBJnt/Xduo/iVJPps1Jxb3takxmsSD4p05C5GZZTXJCa4u/GBurkjFdHhPRqKTVYTnpU= 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-out1.suse.de (Postfix) with ESMTPS id D3D6221BD4 for ; Wed, 3 Jul 2024 13:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1720013032; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=oNCVaVDyuUxLQMDYn5pZQuSMaP7dZ//YZ1f15U14jzw=; b=gbpv1JcNqYzH+ERsbPy6lhf3ms1mW6dGWoCsgNuqO50P4fo8iJlADsxyRngNzVBc6pGBEA 8+Z5QcmQ50oPaCu7NlyWev+rovcjzy8qgvRr6ltGM5Awg7AO0OH+SA6Ydb6SgNEWTUI+JU yXO1Nk8OvABFqb3MC1NycmYxVPCzvrU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1720013032; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=oNCVaVDyuUxLQMDYn5pZQuSMaP7dZ//YZ1f15U14jzw=; b=yPAHvZ/ahx4fY0bReuir7ue8dY/zBOb9/eFHzKIF1fgfAAO64YD+Bb5t2E4KtdbjR1HWF7 iOAwRQaVDkpdbVCg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1720013032; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=oNCVaVDyuUxLQMDYn5pZQuSMaP7dZ//YZ1f15U14jzw=; b=gbpv1JcNqYzH+ERsbPy6lhf3ms1mW6dGWoCsgNuqO50P4fo8iJlADsxyRngNzVBc6pGBEA 8+Z5QcmQ50oPaCu7NlyWev+rovcjzy8qgvRr6ltGM5Awg7AO0OH+SA6Ydb6SgNEWTUI+JU yXO1Nk8OvABFqb3MC1NycmYxVPCzvrU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1720013032; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=oNCVaVDyuUxLQMDYn5pZQuSMaP7dZ//YZ1f15U14jzw=; b=yPAHvZ/ahx4fY0bReuir7ue8dY/zBOb9/eFHzKIF1fgfAAO64YD+Bb5t2E4KtdbjR1HWF7 iOAwRQaVDkpdbVCg== Date: Wed, 3 Jul 2024 15:23:52 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/5] extend SLP load permutation lowering MIME-Version: 1.0 X-Spamd-Result: default: False [-0.26 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_SPAM_LONG(0.54)[0.155]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; 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: -0.26 X-Spam-Level: X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, RCVD_IN_DNSWL_LOW, 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 Message-Id: <20240703132427.19C1A3861011@sourceware.org> The following extends the SLP load permutation to single-level interleaving to handle the case we need multiple levels and adding a fallback handling when the required permutes do not match interleaving but would need three vectors to implement (and thus fail). With the change all single-lane SLP instances should be supported with interleaving (with similar constraints as for non-SLP but not implementing the identical 3 and 5 element group special cases). It also handles void foo (int * __restrict a, int * b) { for (int i = 0; i < 16; ++i) { a[8*i + 0] = b[8*i + 0] * 3; a[8*i + 1] = b[8*i + 1] + 3; a[8*i + 2] = (b[8*i + 2] * 3 + 3); a[8*i + 3] = b[8*i + 3] * 3; a[8*i + 4] = b[8*i + 4] * 3; a[8*i + 5] = b[8*i + 5] + 3; a[8*i + 6] = (b[8*i + 6] * 3 + 3); a[8*i + 7] = b[8*i + 7] * 3; } } albeit with 58 instead of 48 permutes. The non-interleaving fallback needs more work. Next up is supporting gaps. * tree-vect-slp.cc (vect_lower_load_permutations): Support multi-level interleaving. Support non-even/odd permutes. * gcc.dg/vect/slp-11a.c: Expect SLP. * gcc.dg/vect/slp-12a.c: Likewise. --- gcc/testsuite/gcc.dg/vect/slp-11a.c | 2 +- gcc/testsuite/gcc.dg/vect/slp-12a.c | 2 +- gcc/tree-vect-slp.cc | 199 +++++++++++++++++----------- 3 files changed, 122 insertions(+), 81 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/slp-11a.c b/gcc/testsuite/gcc.dg/vect/slp-11a.c index fcb7cf6c7a2..2efa1796757 100644 --- a/gcc/testsuite/gcc.dg/vect/slp-11a.c +++ b/gcc/testsuite/gcc.dg/vect/slp-11a.c @@ -72,4 +72,4 @@ int main (void) /* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" { target { vect_strided8 && vect_int_mult } } } } */ /* { dg-final { scan-tree-dump-times "vectorized 0 loops" 1 "vect" { target { ! { vect_strided8 && vect_int_mult } } } } } */ -/* { dg-final { scan-tree-dump-times "vectorizing stmts using SLP" 0 "vect" } } */ +/* { dg-final { scan-tree-dump-times "vectorizing stmts using SLP" 1 "vect" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/slp-12a.c b/gcc/testsuite/gcc.dg/vect/slp-12a.c index 2f98dc9da0b..fedf27b69d2 100644 --- a/gcc/testsuite/gcc.dg/vect/slp-12a.c +++ b/gcc/testsuite/gcc.dg/vect/slp-12a.c @@ -80,5 +80,5 @@ int main (void) /* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" { target { vect_strided8 && vect_int_mult } } } } */ /* { dg-final { scan-tree-dump-times "vectorized 0 loops" 1 "vect" { target { ! { vect_strided8 && vect_int_mult } } } } } */ -/* { dg-final { scan-tree-dump-times "vectorizing stmts using SLP" 0 "vect" { target { { vect_strided8 && {! vect_load_lanes } } && vect_int_mult } } } } */ +/* { dg-final { scan-tree-dump-times "vectorizing stmts using SLP" 1 "vect" { target { { vect_strided8 && {! vect_load_lanes } } && vect_int_mult } } } } */ /* { dg-final { scan-tree-dump-times "vectorizing stmts using SLP" 0 "vect" { target { ! { vect_strided8 && vect_int_mult } } } } } */ diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index 1d5c4d99549..6f3822af950 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -3993,7 +3993,9 @@ vect_lower_load_permutations (loop_vec_info loop_vinfo, return; group_lanes++; } - /* Only a power-of-two number of lanes matches interleaving. */ + /* Only a power-of-two number of lanes matches interleaving with N levels. + ??? An even number of lanes could be reduced to 1<= group_lanes / 2) continue; - /* Lower by reducing the group to half its size using an - interleaving scheme. For this try to compute whether all - elements needed for this load are in even or odd elements of - an even/odd decomposition with N consecutive elements. - Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition - with N == 2. */ - unsigned even = (1 << ceil_log2 (DR_GROUP_SIZE (first))) - 1; - unsigned odd = even; - for (unsigned l : SLP_TREE_LOAD_PERMUTATION (load)) - { - even &= ~l; - odd &= l; - } - /* Give up when this doesn't match up with an interleaving scheme. */ - if (!even && !odd) - continue; - /* First build (and possibly re-use) a load node for the unpermuted group. */ vec stmts; @@ -4047,66 +4038,105 @@ vect_lower_load_permutations (loop_vec_info loop_vinfo, final_perm.quick_push (std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i])); - /* Now build an even or odd extraction from the unpermuted load. */ - lane_permutation_t perm; - perm.create (group_lanes / 2); - unsigned level; - if (even - && ((level = 1 << ctz_hwi (even)), true) - && group_lanes % (2 * level) == 0) - { - /* { 0, 1, ... 4, 5 ..., } */ - unsigned level = 1 << ctz_hwi (even); - for (unsigned i = 0; i < group_lanes / 2 / level; ++i) - for (unsigned j = 0; j < level; ++j) - perm.quick_push (std::make_pair (0, 2 * i * level + j)); - } - else if (odd) - { - /* { ..., 2, 3, ... 6, 7 } */ - unsigned level = 1 << ctz_hwi (odd); - gcc_assert (group_lanes % (2 * level) == 0); - for (unsigned i = 0; i < group_lanes / 2 / level; ++i) - for (unsigned j = 0; j < level; ++j) - perm.quick_push (std::make_pair (0, (2 * i + 1) * level + j)); - } - else - gcc_unreachable (); - - /* And update final_perm. */ - for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i) + while (1) { - unsigned l = final_perm[i].second; - unsigned j; - for (j = 0; j < perm.length (); ++j) - if (perm[j].second == l) - { - final_perm[i].second = j; - break; - } - gcc_assert (j < perm.length ()); - } + unsigned group_lanes = SLP_TREE_LANES (l0); + if (SLP_TREE_LANES (load) >= group_lanes / 2) + break; - /* And create scalar stmts. */ - vec perm_stmts; - perm_stmts.create (perm.length ()); - for (unsigned i = 0; i < perm.length (); ++i) - perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]); - - slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR); - SLP_TREE_CHILDREN (p).quick_push (l0); - SLP_TREE_LANE_PERMUTATION (p) = perm; - SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load); - SLP_TREE_LANES (p) = perm.length (); - SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load); - /* ??? As we have scalar stmts for this intermediate permute we - could CSE it via bst_map but we do not want to pick up - another SLP node with a load permutation. We instead should - have a "local" CSE map here. */ - SLP_TREE_SCALAR_STMTS (p) = perm_stmts; - /* ??? We need to iterate if group_lanes / 2 is still too large. */ - /* ??? Ideally pick the best even/odd scheme usable for - most of the loads. -> do a multi-step scheme? */ + /* Try to lower by reducing the group to half its size using an + interleaving scheme. For this try to compute whether all + elements needed for this load are in even or odd elements of + an even/odd decomposition with N consecutive elements. + Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition + with N == 2. */ + /* ??? Only an even number of lanes can be handed this way, but the + fallback below could work for any number. */ + gcc_assert ((group_lanes & 1) == 0); + unsigned even = (1 << ceil_log2 (group_lanes)) - 1; + unsigned odd = even; + for (auto l : final_perm) + { + even &= ~l.second; + odd &= l.second; + } + + /* Now build an even or odd extraction from the unpermuted load. */ + lane_permutation_t perm; + perm.create (group_lanes / 2); + unsigned level; + if (even + && ((level = 1 << ctz_hwi (even)), true) + && group_lanes % (2 * level) == 0) + { + /* { 0, 1, ... 4, 5 ..., } */ + unsigned level = 1 << ctz_hwi (even); + for (unsigned i = 0; i < group_lanes / 2 / level; ++i) + for (unsigned j = 0; j < level; ++j) + perm.quick_push (std::make_pair (0, 2 * i * level + j)); + } + else if (odd) + { + /* { ..., 2, 3, ... 6, 7 } */ + unsigned level = 1 << ctz_hwi (odd); + gcc_assert (group_lanes % (2 * level) == 0); + for (unsigned i = 0; i < group_lanes / 2 / level; ++i) + for (unsigned j = 0; j < level; ++j) + perm.quick_push (std::make_pair (0, (2 * i + 1) * level + j)); + } + else + { + /* As fallback extract all used lanes and fill to half the + group size by repeating the last element. + ??? This is quite a bad strathegy for re-use - we could + brute force our way to find more optimal filling lanes to + maximize re-use when looking at all loads from the group. */ + auto_bitmap l; + for (auto p : final_perm) + bitmap_set_bit (l, p.second); + unsigned i = 0; + bitmap_iterator bi; + EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi) + perm.quick_push (std::make_pair (0, i)); + while (perm.length () < group_lanes / 2) + perm.quick_push (perm.last ()); + } + + /* Update final_perm with the intermediate permute. */ + for (unsigned i = 0; i < final_perm.length (); ++i) + { + unsigned l = final_perm[i].second; + unsigned j; + for (j = 0; j < perm.length (); ++j) + if (perm[j].second == l) + { + final_perm[i].second = j; + break; + } + gcc_assert (j < perm.length ()); + } + + /* And create scalar stmts. */ + vec perm_stmts; + perm_stmts.create (perm.length ()); + for (unsigned i = 0; i < perm.length (); ++i) + perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]); + + slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR); + SLP_TREE_CHILDREN (p).quick_push (l0); + SLP_TREE_LANE_PERMUTATION (p) = perm; + SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load); + SLP_TREE_LANES (p) = perm.length (); + SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load); + /* ??? As we have scalar stmts for this intermediate permute we + could CSE it via bst_map but we do not want to pick up + another SLP node with a load permutation. We instead should + have a "local" CSE map here. */ + SLP_TREE_SCALAR_STMTS (p) = perm_stmts; + + /* We now have a node for group_lanes / 2 lanes. */ + l0 = p; + } /* And finally from the ordered reduction node create the permute to shuffle the lanes into the original load-permutation @@ -4115,7 +4145,7 @@ vect_lower_load_permutations (loop_vec_info loop_vinfo, SLP_TREE_LOAD_PERMUTATION (load).release (); SLP_TREE_LANE_PERMUTATION (load) = final_perm; SLP_TREE_CHILDREN (load).create (1); - SLP_TREE_CHILDREN (load).quick_push (p); + SLP_TREE_CHILDREN (load).quick_push (l0); } } @@ -4315,7 +4345,18 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) like those requiring three vector inputs, lower them using interleaving like schemes. */ if (loop_vec_info loop_vinfo = dyn_cast (vinfo)) - vect_lower_load_permutations (loop_vinfo, bst_map); + { + vect_lower_load_permutations (loop_vinfo, bst_map); + if (dump_enabled_p ()) + { + dump_printf_loc (MSG_NOTE, vect_location, + "SLP graph after lowering permutations:\n"); + hash_set visited; + FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance) + vect_print_slp_graph (MSG_NOTE, vect_location, + SLP_INSTANCE_TREE (instance), visited); + } + } hash_set visited_patterns; slp_tree_to_load_perm_map_t perm_cache;