From patchwork Wed Jun 19 12:27:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1949670 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=TZKQkqVY; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=/ITZlZ/I; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=TZKQkqVY; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=/ITZlZ/I; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4W42v452qhz20KL for ; Wed, 19 Jun 2024 22:27:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6BCE538845A5 for ; Wed, 19 Jun 2024 12:27:25 +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 938013884546 for ; Wed, 19 Jun 2024 12:27:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 938013884546 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 938013884546 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=1718800025; cv=none; b=eWfZDKRWfAv6f7FMaHvA19Q/7IUgvem8T0NUNkrn1uSdA6gKDNqgSJWTRr1FIx9fTiEJt7BC1BARay5/GTZds6yj8cMa5nPLp/PsDud3ObKb+3Os1i4WvnCqrdJ83ET9+PUscDyCDAhZn+JKsbyaCaSu1wPiwGeSCJDONuSIBs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718800025; c=relaxed/simple; bh=mT2Wq+hnV1U7yFwIHgeEvhazrYrdbKTf4dpaobzFiVc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=Aj1HHotRLmek6cbYy8Iu0+d8U0VASptItoU4jShAV3unuPEX26m8AheFs6Qf2Cvs2V3l8IQiTYZ5Z/WOhQKemRaevX5UjwLGUUUKo5iFwUmUA9OpTsVut6u9gQqRc5BegAzQ/A7kXUyfrwqkW0lVA1TrlH/HCvmeBXap7ahVTuc= 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 71E1521A9E; Wed, 19 Jun 2024 12:27:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1718800022; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=LBHK4OjtSSor2JPfPHwKGKcCcsFOjj++F97Fy8KPmow=; b=TZKQkqVYsR92psriCSzi6TPUuq+pvLEurOotiGttzZMQjhnywiimD8Te9pJT878lYSL/Fl u0VG3sYjjGQj4GZ1Vn/JpEH21vv800ZwnhigSK8799ROvyGdN2Dq+hCVayaUf1RAhpvNWA 8M6BIg/zdo7QtM+6XiglJ3dnmC5HJVI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1718800022; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=LBHK4OjtSSor2JPfPHwKGKcCcsFOjj++F97Fy8KPmow=; b=/ITZlZ/Il0v/MQRRQOhlaZx2jboI7yrU6fX5a/jtknXlz8+0oXHvjWbb3duTjLR57EYyow uaWgQSuLRQwQNxBA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1718800022; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=LBHK4OjtSSor2JPfPHwKGKcCcsFOjj++F97Fy8KPmow=; b=TZKQkqVYsR92psriCSzi6TPUuq+pvLEurOotiGttzZMQjhnywiimD8Te9pJT878lYSL/Fl u0VG3sYjjGQj4GZ1Vn/JpEH21vv800ZwnhigSK8799ROvyGdN2Dq+hCVayaUf1RAhpvNWA 8M6BIg/zdo7QtM+6XiglJ3dnmC5HJVI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1718800022; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=LBHK4OjtSSor2JPfPHwKGKcCcsFOjj++F97Fy8KPmow=; b=/ITZlZ/Il0v/MQRRQOhlaZx2jboI7yrU6fX5a/jtknXlz8+0oXHvjWbb3duTjLR57EYyow uaWgQSuLRQwQNxBA== Date: Wed, 19 Jun 2024 14:27:02 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: richard.sandiford@arm.com Subject: [PATCH] tree-optimization/114413 - SLP CSE after permute optimization MIME-Version: 1.0 X-Spamd-Result: default: False [-1.79 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_LONG(-0.99)[-0.987]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FROM_HAS_DN(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; MISSING_XM_UA(0.00)[]; ARC_NA(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; MIME_TRACE(0.00)[0:+] X-Spam-Score: -1.79 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: <20240619122725.6BCE538845A5@sourceware.org> We currently fail to re-CSE SLP nodes after optimizing permutes which results in off cost estimates. For gcc.dg/vect/bb-slp-32.c this shows in not re-using the SLP node with the load and arithmetic for both the store and the reduction. The following implements CSE by re-bst-mapping nodes as finalization part of vect_optimize_slp. I've tried to make the CSE part of permute materialization but it isn't a very good fit there. I've not bothered to implement something more complete, also handling external defs or defs without SLP_TREE_SCALAR_STMTS. I realize this might result in more BB SLP which in turn might slow down code given costing for BB SLP is difficult (even that we now vectorize gcc.dg/vect/bb-slp-32.c on x86_64 might be not a good idea). This is nevertheless feeding more accurate info to costing which is good. Bootstrapped and tested on x86_64-unknown-linux-gnu. Does this look OK? Thanks, Richard. PR tree-optimization/114413 * tree-vect-slp.cc (release_scalar_stmts_to_slp_tree_map): New function, split out from ... (vect_analyze_slp): ... here. Call it. (vect_cse_slp_nodes): New function. (vect_optimize_slp): Call it. * gcc.dg/vect/bb-slp-32.c: Expect CSE and vectorization on x86. --- gcc/testsuite/gcc.dg/vect/bb-slp-32.c | 6 +++ gcc/tree-vect-slp.cc | 77 ++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-32.c b/gcc/testsuite/gcc.dg/vect/bb-slp-32.c index 4f72727b694..475b241c36e 100644 --- a/gcc/testsuite/gcc.dg/vect/bb-slp-32.c +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-32.c @@ -38,3 +38,9 @@ int main() abort (); return 0; } + +/* This is a weak test but we want to re-use the arithmetic for both the + store and the reduction. */ +/* { dg-final { scan-tree-dump "re-using SLP tree" "slp2" { target { x86_64-*-* i?86-*-* } } } } */ +/* On i386 we vectorize both the store and the reduction. */ +/* { dg-final { scan-tree-dump-times "basic block part vectorized" 2 "slp2" { target { x86_64-*-* i?86-*-* } } } } */ diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index 2552dacbd69..980d1e7267d 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -1586,6 +1586,23 @@ bst_traits::equal (value_type existing, value_type candidate) return true; } +typedef hash_map , slp_tree, + simple_hashmap_traits > + scalar_stmts_to_slp_tree_map_t; + +/* Release BST_MAP. */ + +static void +release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map) +{ + /* The map keeps a reference on SLP nodes built, release that. */ + for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin (); + it != bst_map->end (); ++it) + if ((*it).second) + vect_free_slp_tree ((*it).second); + delete bst_map; +} + /* ??? This was std::pair, tree> but then vec::insert does memmove and that's not compatible with std::pair. */ @@ -1684,10 +1701,6 @@ vect_slp_linearize_chain (vec_info *vinfo, } } -typedef hash_map , slp_tree, - simple_hashmap_traits > - scalar_stmts_to_slp_tree_map_t; - static slp_tree vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node, vec stmts, unsigned int group_size, @@ -4308,14 +4321,7 @@ vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size) } } - - - /* The map keeps a reference on SLP nodes built, release that. */ - for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin (); - it != bst_map->end (); ++it) - if ((*it).second) - vect_free_slp_tree ((*it).second); - delete bst_map; + release_scalar_stmts_to_slp_tree_map (bst_map); if (pattern_found && dump_enabled_p ()) { @@ -6373,6 +6379,43 @@ vect_optimize_slp_pass::run () free_graph (m_slpg); } +/* Apply CSE to NODE and its children using BST_MAP. */ + +static void +vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node) +{ + if (SLP_TREE_DEF_TYPE (node) == vect_internal_def + && SLP_TREE_CODE (node) != VEC_PERM_EXPR + /* Besides some VEC_PERM_EXPR, two-operator nodes also + lack scalar stmts and thus CSE doesn't work via bst_map. Ideally + we'd have sth that works for all internal and external nodes. */ + && !SLP_TREE_SCALAR_STMTS (node).is_empty ()) + { + if (slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node))) + { + if (*leader != node) + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_NOTE, vect_location, + "re-using SLP tree %p for %p\n", + (void *)*leader, (void *)node); + vect_free_slp_tree (node); + (*leader)->refcnt += 1; + node = *leader; + } + return; + } + + bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), node); + node->refcnt += 1; + /* And recurse. */ + } + + for (slp_tree &child : SLP_TREE_CHILDREN (node)) + if (child) + vect_cse_slp_nodes (bst_map, child); +} + /* Optimize the SLP graph of VINFO. */ void @@ -6381,6 +6424,16 @@ vect_optimize_slp (vec_info *vinfo) if (vinfo->slp_instances.is_empty ()) return; vect_optimize_slp_pass (vinfo).run (); + + + /* Apply CSE again to nodes after permute optimization. */ + scalar_stmts_to_slp_tree_map_t *bst_map + = new scalar_stmts_to_slp_tree_map_t (); + + for (auto inst : vinfo->slp_instances) + vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst)); + + release_scalar_stmts_to_slp_tree_map (bst_map); } /* Gather loads reachable from the individual SLP graph entries. */