From patchwork Fri Mar 1 09:17:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 208720 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp957205dyb; Fri, 1 Mar 2024 01:21:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV++FbbK3WSTw/NQyQJ15sKUiamQdgbRObQwflDpdrhohFBnEVbou7ze9L/dOBVUgOpkK7I07vfipIQi39QpM13sVenuw== X-Google-Smtp-Source: AGHT+IHrpfjdE3DN/TgrAhFSR0vtuXWqn7UmZbYrTORmCn1rzI3bEFXhZx/UBrBtk5Fjimv/UIR6 X-Received: by 2002:ac8:5c55:0:b0:42e:cc32:9dd0 with SMTP id j21-20020ac85c55000000b0042ecc329dd0mr761995qtj.59.1709284887162; Fri, 01 Mar 2024 01:21:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709284887; cv=pass; d=google.com; s=arc-20160816; b=cW3J17zRy0ML1eTz1SGaCd6vTT+qv8A9CFrtetqayMQnC3CoQmZtBn+/AJ4oNTNglT 7gXyKKwoPezJvC9kpYvvYidrc4I8a+ArzDmkaa5FQeAhZCsyyhM1RcANEQ0IWIXCucOR H8LmpjABUyDPvL3zVYrhhWoccVm9/bKmX1LkvOgok9ISyVuPQ3Wz4VRzlbGfVlHGgu6T ESQ8X9EYvme5Sy3wqfdQvASLtktmwhgFEhshmZfLWPqwZUId0WEGQBbr9/Ld/Q9UCHVy Htbd4o8M3+MJoXcEChhDgn3OxQfW6allAlWWx/MDe1Z4JOEopwzRIqyom5ordY14gMAv Q55w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=PDQQPwTlosKERTVunzyzFLdiiMydq0hi5LxXupWlUKg=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=r+PPJ0/XA5xxS9s1EPYrzHOeRIVBRh4JKCD2Dpw+GmwkcQgrULLP806qmPAcK7jWPY gythS0CaInU1J9Cl9hqwR3VnFaNxTVb9OIPUWrnagyUmNQQ+3covkQ/TBcGm1hZanl9w RhpPDpL/qI8pIqrBK7rC96L7cyfadRYQLCECRhhXlzGT6EbtyF37lZvJn/tcBcxtB+TK +mRaUXqIZ/fdSzoArtRYj2kF09W5SMgd4sVRAiG1KSCyUg161mYLeW5LSe+speJMAP9W SXJZmu5+EaP2zXKyAE7Mc8Rd9iDWxrf2GvQ38pj41xAi+GXSehJQn+g0WCVsmsEQlL66 uQcw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=az8q9dTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=az8q9dTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g13-20020ac87d0d000000b0042ea04dddd6si3402259qtb.747.2024.03.01.01.21.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 01:21:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=az8q9dTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=az8q9dTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BA94F3858401 for ; Fri, 1 Mar 2024 09:21:26 +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 EF9DC3858D39 for ; Fri, 1 Mar 2024 09:17:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF9DC3858D39 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 EF9DC3858D39 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=1709284686; cv=none; b=YsS1mUlUGvKEWh0Y479mz2PfiNb/b/3U6nm+pcyo0FqUwjo+D9i8liK1pOw1ZCtr4RIYNhi1fwWR3KuhI2ME1j17tSnlBTpHiydpPcY1CoKwuQLbqjq0vxgg5SItwbpFoctUgRdLrD38eCVddjImI8DTN70g6tSyN1PcUaReH9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709284686; c=relaxed/simple; bh=Rc60lPddRpHXMikvdZKReNBq4w6mTSGdQYjBDuR16uA=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=v+8nsE/cxylA3It/Nu3gUwjgJtFuI2lIJdoQrVtgSFGAALHc1z0IZhsprc7ggL3+fczI/mcAum6WXREdJCUOobIuk5CuHE7Sls1Cj9rSG6YzFlihz33fscCmunkLVG06To8HfDARFXQpN4Lg8sJD2q9vzX27VJdMhGtHHKQ6pko= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (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 A9B171FFFA for ; Fri, 1 Mar 2024 09:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709284655; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=PDQQPwTlosKERTVunzyzFLdiiMydq0hi5LxXupWlUKg=; b=az8q9dTcEJBt/zMYudQUNpdutlA4d3YRO5Saku3H/ZpjCSL01bq7YWbPzKcu0kXNrYH0UC UV4uY0zFf1RfeaqvvS0izQoPzY9WohwqIwTxiiQeFeoLtjvGgqwFfOmUjTnqyG6O+R+15c 1334N0ewHJzfUZ7pFh3TReVnEskDw5A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709284655; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=PDQQPwTlosKERTVunzyzFLdiiMydq0hi5LxXupWlUKg=; b=zulPwStchAAnGRBWMglg+YBhBvBTs7l9LDWAfZ1HV3v8q5MJI7c0f8tVJVkYs32Mrvy+gC Hpnt5StjfdrZ2gCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709284655; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=PDQQPwTlosKERTVunzyzFLdiiMydq0hi5LxXupWlUKg=; b=az8q9dTcEJBt/zMYudQUNpdutlA4d3YRO5Saku3H/ZpjCSL01bq7YWbPzKcu0kXNrYH0UC UV4uY0zFf1RfeaqvvS0izQoPzY9WohwqIwTxiiQeFeoLtjvGgqwFfOmUjTnqyG6O+R+15c 1334N0ewHJzfUZ7pFh3TReVnEskDw5A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709284655; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=PDQQPwTlosKERTVunzyzFLdiiMydq0hi5LxXupWlUKg=; b=zulPwStchAAnGRBWMglg+YBhBvBTs7l9LDWAfZ1HV3v8q5MJI7c0f8tVJVkYs32Mrvy+gC Hpnt5StjfdrZ2gCg== Received: from imap2.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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 8BBBD13A39 for ; Fri, 1 Mar 2024 09:17:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id 5l0eIC+d4WX4EQAAn2gu4w (envelope-from ) for ; Fri, 01 Mar 2024 09:17:35 +0000 Date: Fri, 1 Mar 2024 10:17:35 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Allow patterns in SLP reductions MIME-Version: 1.0 Message-Id: <20240301091735.8BBBD13A39@imap2.dmz-prg2.suse.org> Authentication-Results: smtp-out2.suse.de; none X-Spamd-Result: default: False [-3.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_THREE(0.00)[3]; 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:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Score: -3.10 X-Spam-Status: No, score=-11.0 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, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792315110147794356 X-GMAIL-MSGID: 1792315110147794356 The following removes the over-broad rejection of patterns for SLP reductions which is done by removing them from LOOP_VINFO_REDUCTIONS during pattern detection. That's also insufficient in case the pattern only appears on the reduction path. Instead this implements the proper correctness check in vectorizable_reduction and guides SLP discovery to heuristically avoid forming later invalid groups. I also couldn't find any testcase that FAILs when allowing the SLP reductions to form so I've added one. I came across this for single-lane SLP reductions with the all-SLP work where we rely on patterns to properly vectorize COND_EXPR reductions. Bootstrapped and tested on x86_64-unknown-linux-gnu, queued for stage1. Richard. * tree-vect-patterns.cc (vect_pattern_recog_1): Do not remove reductions involving patterns. * tree-vect-loop.cc (vectorizable_reduction): Reject SLP reduction groups with multiple lane-reducing reductions. * tree-vect-slp.cc (vect_analyze_slp_instance): When discovering SLP reduction groups avoid including lane-reducing ones. * gcc.dg/vect/vect-reduc-sad-9.c: New testcase. --- gcc/testsuite/gcc.dg/vect/vect-reduc-sad-9.c | 68 ++++++++++++++++++++ gcc/tree-vect-loop.cc | 15 +++++ gcc/tree-vect-patterns.cc | 13 ---- gcc/tree-vect-slp.cc | 26 +++++--- 4 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-sad-9.c diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-sad-9.c b/gcc/testsuite/gcc.dg/vect/vect-reduc-sad-9.c new file mode 100644 index 00000000000..3c6af4510f4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-sad-9.c @@ -0,0 +1,68 @@ +/* Disabling epilogues until we find a better way to deal with scans. */ +/* { dg-additional-options "--param vect-epilogues-nomask=0" } */ +/* { dg-additional-options "-msse4.2" { target { x86_64-*-* i?86-*-* } } } */ +/* { dg-require-effective-target vect_usad_char } */ + +#include +#include "tree-vect.h" + +#define N 64 + +unsigned char X[N] __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))); +unsigned char Y[N] __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))); +int abs (int); + +/* Sum of absolute differences between arrays of unsigned char types. + Detected as a sad pattern. + Vectorized on targets that support sad for unsigned chars. */ + +__attribute__ ((noinline)) int +foo (int len, int *res2) +{ + int i; + int result = 0; + int result2 = 0; + + for (i = 0; i < len; i++) + { + /* Make sure we are not using an SLP reduction for this. */ + result += abs (X[2*i] - Y[2*i]); + result2 += abs (X[2*i + 1] - Y[2*i + 1]); + } + + *res2 = result2; + return result; +} + + +int +main (void) +{ + int i; + int sad; + + check_vect (); + + for (i = 0; i < N/2; i++) + { + X[2*i] = i; + Y[2*i] = N/2 - i; + X[2*i+1] = i; + Y[2*i+1] = 0; + __asm__ volatile (""); + } + + + int sad2; + sad = foo (N/2, &sad2); + if (sad != (N/2)*(N/4)) + abort (); + if (sad2 != (N/2-1)*(N/2)/2) + abort (); + + return 0; +} + +/* { dg-final { scan-tree-dump "vect_recog_sad_pattern: detected" "vect" } } */ +/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" } } */ + diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 35f1f8c7d42..13dcdba403a 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -7703,6 +7703,21 @@ vectorizable_reduction (loop_vec_info loop_vinfo, return false; } + /* Lane-reducing ops also never can be used in a SLP reduction group + since we'll mix lanes belonging to different reductions. But it's + OK to use them in a reduction chain or when the reduction group + has just one element. */ + if (lane_reduc_code_p + && slp_node + && !REDUC_GROUP_FIRST_ELEMENT (stmt_info) + && SLP_TREE_LANES (slp_node) > 1) + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "lane-reducing reduction in reduction group.\n"); + return false; + } + /* All uses but the last are expected to be defined in the loop. The last use is the reduction variable. In case of nested cycle this assumption is not true: we use reduc_index to record the index of the diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc index d562f57920f..fe1ffba8688 100644 --- a/gcc/tree-vect-patterns.cc +++ b/gcc/tree-vect-patterns.cc @@ -7172,7 +7172,6 @@ vect_pattern_recog_1 (vec_info *vinfo, vect_recog_func *recog_func, stmt_vec_info stmt_info) { gimple *pattern_stmt; - loop_vec_info loop_vinfo; tree pattern_vectype; /* If this statement has already been replaced with pattern statements, @@ -7198,8 +7197,6 @@ vect_pattern_recog_1 (vec_info *vinfo, return; } - loop_vinfo = dyn_cast (vinfo); - /* Found a vectorizable pattern. */ if (dump_enabled_p ()) dump_printf_loc (MSG_NOTE, vect_location, @@ -7208,16 +7205,6 @@ vect_pattern_recog_1 (vec_info *vinfo, /* Mark the stmts that are involved in the pattern. */ vect_mark_pattern_stmts (vinfo, stmt_info, pattern_stmt, pattern_vectype); - - /* Patterns cannot be vectorized using SLP, because they change the order of - computation. */ - if (loop_vinfo) - { - unsigned ix, ix2; - stmt_vec_info *elem_ptr; - VEC_ORDERED_REMOVE_IF (LOOP_VINFO_REDUCTIONS (loop_vinfo), ix, ix2, - elem_ptr, *elem_ptr == stmt_info); - } } diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index dabd8407aaf..d9961945c1c 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -3597,14 +3597,24 @@ vect_analyze_slp_instance (vec_info *vinfo, = as_a (vinfo)->reductions; scalar_stmts.create (reductions.length ()); for (i = 0; reductions.iterate (i, &next_info); i++) - if ((STMT_VINFO_RELEVANT_P (next_info) - || STMT_VINFO_LIVE_P (next_info)) - /* ??? Make sure we didn't skip a conversion around a reduction - path. In that case we'd have to reverse engineer that conversion - stmt following the chain using reduc_idx and from the PHI - using reduc_def. */ - && STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def) - scalar_stmts.quick_push (next_info); + { + gassign *g; + next_info = vect_stmt_to_vectorize (next_info); + if ((STMT_VINFO_RELEVANT_P (next_info) + || STMT_VINFO_LIVE_P (next_info)) + /* ??? Make sure we didn't skip a conversion around a reduction + path. In that case we'd have to reverse engineer that + conversion stmt following the chain using reduc_idx and from + the PHI using reduc_def. */ + && STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def + /* Do not discover SLP reductions for lane-reducing ops, that + will fail later. */ + && (!(g = dyn_cast (STMT_VINFO_STMT (next_info))) + || (gimple_assign_rhs_code (g) != DOT_PROD_EXPR + && gimple_assign_rhs_code (g) != WIDEN_SUM_EXPR + && gimple_assign_rhs_code (g) != SAD_EXPR))) + scalar_stmts.quick_push (next_info); + } /* If less than two were relevant/live there's nothing to SLP. */ if (scalar_stmts.length () < 2) return false;