From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id E20CF384384A for ; Fri, 10 May 2024 13:06:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E20CF384384A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E20CF384384A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715346409; cv=none; b=uHIcEnlN0/p8QJirjgvtfxm2DTXUcW/dSSaWXASq/KTMSNr+rY2JyywA/qvlc1Jj8EeXx8RXm9v5GGctQF332tc2XwA0rH9QczFr3LXezNTyDI9J9XaaxnI0J3BMmZbsuVPJvC735Bha3Z6BEpccjcwSI0oeQB0P9Yyx4jPSlI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715346409; c=relaxed/simple; bh=5QMUDmRkCdSjsiPwBuNaJ3Eiicu89WLFgTkn6Rb0tgk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=EhUYE061SeFfperYeFxMe4k7lhEpu5JDkfmvkLQ/PzA+1kaiH/3DB5lEkJGZHSDV1+tP1hyivb1LdMLW6EqjBsxJCNT33Eggf3glvpUOiY8y5/Y6peBTaEBqgxNlI6h+9aQJmA65z7E6bSAwcQBfLxFtsZU40eiIrGJqeIedegI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-521ea6bf76eso2237287e87.0 for ; Fri, 10 May 2024 06:06:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715346404; x=1715951204; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xPCv84EXgrfdCqlAm7fUuGknSYZzLEkJ+TMYA7Zy8QQ=; b=ntO8XnkiTGahl+rYs59nIY+JtX9xCu/W+MvQPywKwgbXm5UQsjMyp2XKJ2aXQMX0Jk HFdFTHHmJ3mogKAcnT8vut+5oR+k1cl78tO5hiDMpH/7c39vXdW4qw4GnSh7Cl17wpGS Pf5tn8X+EweY1IDKjOWdi8uZ8sfxKcXXxI/UXVtbTytukEzSCkJMPbjIJx/3IgpjmFTZ Nw9fJ7cHkmgMRprXqhqaGlL2BB7PMMZ8CTA/0ZA7h224oiYTXxXDzNGYR823GtMLQ6kO ZnXQtldv+41OBHMoa308jr+5BRj/1UvedXW9oR9AI4yne/zY1X9PgKb2QqRYPdyQRITN 88bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715346404; x=1715951204; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xPCv84EXgrfdCqlAm7fUuGknSYZzLEkJ+TMYA7Zy8QQ=; b=btG6iZTtGA+8q4UmwAYXtXPR8vGdugm7tn+m9QIENiFT3ExJLoMKJx2pclSsb9jndJ l5H4gdQd0tZHlkgwqqte0Sh9l8gXRsJ0BByTMmUcmL7MGl0NVXPei76CgZdVCce4+CfX 9mYOdFDq2xzIAp+eOHXkOw0D+V6dvV7ACJCxgRISnSGr2IAYb2zjaNUqylmo7yFP1dg/ 3PHJfj2pPcDUeMhWasZt2LlL7JwIni2Nfzh+meinMovMPyr1xTLv7sPFVObmy0fceru4 M+1EJ5OQx4bfuQ3+krLVGeEjlwOKF2/ElOUoHjC4BELQ/+GY0mMixshzQdntEZ2RX/gD w5DA== X-Gm-Message-State: AOJu0Yxegzs53LzwSREOOr4jfITwXTbkshR/XQfWiDoNTVXW7KP9ZxIu M/Qhg30TWkhAciOqY4MbEMmHfE0MEjpJ9up4jLhK5fzRPrw87KK0kdAQjLCQ/7itFT4+Ej65lQh HMVqzKmVo7N045ydP7XuS8CDhrdAKURsG X-Google-Smtp-Source: AGHT+IF709Jfn/SrmolwbqIQ4pE4J7yO2OK3SLz2Vbzu5gDuB5VT10KbzmENRyceUx6m5OJl/i93jI6DeBw27jaBqEo= X-Received: by 2002:a05:6512:33c5:b0:522:2a2c:759e with SMTP id 2adb3069b0e04-5222a2c76c2mr613115e87.6.1715346404058; Fri, 10 May 2024 06:06:44 -0700 (PDT) MIME-Version: 1.0 References: <20240301091735.8BBBD13A39@imap2.dmz-prg2.suse.org> In-Reply-To: <20240301091735.8BBBD13A39@imap2.dmz-prg2.suse.org> From: Richard Biener Date: Fri, 10 May 2024 15:06:33 +0200 Message-ID: Subject: Re: [PATCH] Allow patterns in SLP reductions To: Richard Biener Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: On Fri, Mar 1, 2024 at 10:21=E2=80=AFAM Richard Biener = wrote: > > 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. Re-bootstrapped/tested, r15-361-g52d4691294c847 Richard. > 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=3D0" } */ > +/* { 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 =3D 0; > + int result2 =3D 0; > + > + for (i =3D 0; i < len; i++) > + { > + /* Make sure we are not using an SLP reduction for this. */ > + result +=3D abs (X[2*i] - Y[2*i]); > + result2 +=3D abs (X[2*i + 1] - Y[2*i + 1]); > + } > + > + *res2 =3D result2; > + return result; > +} > + > + > +int > +main (void) > +{ > + int i; > + int sad; > + > + check_vect (); > + > + for (i =3D 0; i < N/2; i++) > + { > + X[2*i] =3D i; > + Y[2*i] =3D N/2 - i; > + X[2*i+1] =3D i; > + Y[2*i+1] =3D 0; > + __asm__ volatile (""); > + } > + > + > + int sad2; > + sad =3D foo (N/2, &sad2); > + if (sad !=3D (N/2)*(N/4)) > + abort (); > + if (sad2 !=3D (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 th= is > assumption is not true: we use reduc_index to record the index of t= he > 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_inf= o) > { > 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 =3D 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_vecty= pe); > - > - /* Patterns cannot be vectorized using SLP, because they change the or= der 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 =3D=3D 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, > =3D as_a (vinfo)->reductions; > scalar_stmts.create (reductions.length ()); > for (i =3D 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 reduct= ion > - path. In that case we'd have to reverse engineer that con= version > - stmt following the chain using reduc_idx and from the PHI > - using reduc_def. */ > - && STMT_VINFO_DEF_TYPE (next_info) =3D=3D vect_reduction_def) > - scalar_stmts.quick_push (next_info); > + { > + gassign *g; > + next_info =3D 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 redu= ction > + 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) =3D=3D vect_reduction_de= f > + /* Do not discover SLP reductions for lane-reducing ops, th= at > + will fail later. */ > + && (!(g =3D dyn_cast (STMT_VINFO_STMT (next_inf= o))) > + || (gimple_assign_rhs_code (g) !=3D DOT_PROD_EXPR > + && gimple_assign_rhs_code (g) !=3D WIDEN_SUM_EXPR > + && gimple_assign_rhs_code (g) !=3D 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; > -- > 2.35.3